File tree 8 files changed +96
-14
lines changed
8 files changed +96
-14
lines changed Original file line number Diff line number Diff line change 1
- B100
2
- true
3
- 10.2
4
1
Bhuwan
2
+ Hello BufferWriter!!
3
+ ABC
4
+ X
Original file line number Diff line number Diff line change @@ -19,7 +19,7 @@ public class BoundedType {
19
19
public static <T extends Comparable <T >> int compare (T t1 , T t2 ) {
20
20
return t1 .compareTo (t2 );
21
21
}
22
-
22
+
23
23
@ SuppressWarnings ("unused" )
24
24
public static void main (String [] args ) {
25
25
String name = "Bhuwan" ;
Original file line number Diff line number Diff line change @@ -26,4 +26,22 @@ public static void One(ArrayList<String> s) {
26
26
// public static void One(ArrayList<Integer> s) {
27
27
// }
28
28
29
+ public <T extends Happy > T foo (T emotion ) {
30
+ // do something
31
+ return null ;
32
+ }
33
+
34
+ public <T extends Sad > T foo (T emotion ) {
35
+ return null ;
36
+ // do something else
37
+ }
38
+
39
+ }
40
+
41
+ class Happy {
42
+
43
+ }
44
+
45
+ class Sad {
46
+
29
47
}
Original file line number Diff line number Diff line change @@ -17,7 +17,7 @@ public static void main(String[] args) {
17
17
// this is not same with generic
18
18
// object = nameString;
19
19
20
- InheritanceDemo3 < String > abc = new InheritanceDemo3 ();
20
+ InheritanceDemo4 < CustomRunnableDemo > abc = new InheritanceDemo4 ();
21
21
InheritanceDemo3 <Object > xyz = new InheritanceDemo3 ();
22
22
}
23
23
}
@@ -37,6 +37,11 @@ class InheritanceDemo2<T extends Number & Comparable & Runnable> {
37
37
38
38
}
39
39
40
+ // syntax three
41
+ class InheritanceDemo4 <T extends Runnable > {
42
+
43
+ }
44
+
40
45
/**
41
46
* syntax four
42
47
*
@@ -46,6 +51,10 @@ class InheritanceDemo2<T extends Number & Comparable & Runnable> {
46
51
// }
47
52
48
53
// syntax five
49
- class InheritanceDemo3 <T >{
50
-
54
+ class InheritanceDemo3 <T > {
55
+
56
+ }
57
+
58
+ class CustomRunnableDemo extends Thread {
59
+
51
60
}
Original file line number Diff line number Diff line change @@ -13,7 +13,7 @@ public class GenericsLowerBounded {
13
13
14
14
public static void main (String [] args ) {
15
15
// it only accepts Number or Object type
16
- List <Number > ints = new ArrayList <>();
16
+ List <Object > ints = new ArrayList <>();
17
17
ints .add (3 );
18
18
ints .add (5 );
19
19
ints .add (10 );
Original file line number Diff line number Diff line change
1
+ /**
2
+ *
3
+ */
4
+ package com .bhuwan .java .generics ;
5
+
6
+ import java .util .ArrayList ;
7
+ import java .util .List ;
8
+
9
+ /**
10
+ * @author bhuwan
11
+ *
12
+ */
13
+ public class TestGenerics {
14
+
15
+ public static void main (String [] args ) {
16
+ List list = new ArrayList ();
17
+ list .add (123 );
18
+ list .add ("Bhuwan" );
19
+ list .add (12.22 );
20
+ list .add (123.1f );
21
+
22
+ Double name = (Double ) list .get (1 );
23
+
24
+ System .out .println (list );
25
+ System .out .println (name );
26
+
27
+ List <Integer > cities = new ArrayList <>();
28
+ cities .add (1232 );
29
+
30
+ CustomGenerics <Integer > numbers = new CustomGenerics <>();
31
+ numbers .setT (1223 );
32
+ }
33
+
34
+ }
35
+
36
+ class CustomGenerics <T extends Number > {
37
+
38
+ private T t ;
39
+
40
+ /**
41
+ * @return the t
42
+ */
43
+ public T getT () {
44
+ return t ;
45
+ }
46
+
47
+ /**
48
+ * @param t
49
+ * the t to set
50
+ */
51
+ public void setT (T t ) {
52
+ this .t = t ;
53
+ }
54
+
55
+ }
Original file line number Diff line number Diff line change 11
11
*/
12
12
public class UsingGenerics {
13
13
14
- private static final Logger log = Logger .getLogger (WithoutUsingGenerics .class .getName ());
14
+ private static final Logger log = Logger .getLogger (UsingGenerics .class .getName ());
15
15
16
16
public static void main (String args []) {
17
17
@@ -20,10 +20,10 @@ public static void main(String args[]) {
20
20
21
21
// add marks
22
22
marksList .add (94.0 );
23
- //marksList.add("Hundred");
23
+ // marksList.add("Hundred");
24
24
marksList .add (90.0 );
25
- //compile-time error
26
- //marksList.add(72);
25
+ // compile-time error
26
+ // marksList.add(72);
27
27
28
28
log .info ("Percentage:: " + calculatePercentage (marksList ));
29
29
}
Original file line number Diff line number Diff line change @@ -19,9 +19,9 @@ public class BufferedWriterDemo {
19
19
*/
20
20
public static void main (String [] args ) throws IOException {
21
21
try (BufferedWriter bw = new BufferedWriter (new FileWriter ("file_writer_demo.txt" , true ));
22
- BufferedWriter bw1 = new BufferedWriter (new FileWriter ("file_writer_demo .txt" ));
22
+ BufferedWriter bw1 = new BufferedWriter (new FileWriter ("file_writer_demo1 .txt" ));
23
23
// initializing double buffered writer object.
24
- BufferedWriter bw2 = new BufferedWriter (new BufferedWriter (new FileWriter ("file_writer_demo .txt" )));) {
24
+ BufferedWriter bw2 = new BufferedWriter (new BufferedWriter (new FileWriter ("file_writer_demo1 .txt" )));) {
25
25
bw .write (66 );
26
26
bw .write ("huwan" );
27
27
bw .newLine ();
You can’t perform that action at this time.
0 commit comments