@@ -9,24 +9,84 @@ class Machine1 {
99 public String toString () {
1010 return "This is a Machine." ;
1111 }
12+ public void start () {
13+ System .out .println ("Machine starting..." );
14+ }
1215
16+ public void stop () {
17+ System .out .println ("Machine stopping..." );
18+ }
1319}
1420
1521class Camera extends Machine1 {
1622 @ Override
1723 public String toString () {
1824 return "This is a Camera." ;
1925 }
26+ // Overrides the start method in Machine1 class
27+ public void start () {
28+ System .out .println ("Camera starting..." );
29+ }
30+ public void snap () {
31+ System .out .println ("Photo snapped.." );
32+ }
33+ }
34+
35+ class Car extends Machine1 {
36+ @ Override
37+ public String toString () {
38+ return "This is a Car." ;
39+ }
40+ public void stall () {
41+ System .out .println ("Car has stalled..." );
42+ }
43+ public void sleep () {
44+ System .out .println ("Car is sleeping..." );
45+ }
46+ }
47+
48+ class Bus extends Machine1 {
49+ @ Override
50+ public String toString () {
51+ return "This is a Bus." ;
52+ }
53+ // Overrides the start method in Machine1 class
54+ public void start () {
55+ System .out .println ("Bus has started..." );
56+ }
57+ // Overrides the stop method in Machine1 class
58+ public void stop () {
59+ System .out .println ("Bus has stopped..." );
60+ }
2061}
2162
2263public class GenericsWildcard {
2364
65+ // Used wildcard "?" as to pass object of any class
2466 public static void showList (ArrayList <?> list ) {
67+
68+ // Using Object as it is the super class
2569 for (Object value : list ) {
2670 System .out .println (value );
2771 }
2872 }
2973
74+ public static void showListExtends (ArrayList <? extends Machine1 > list ) {
75+
76+ // Changing to Machine1 from Object as this method
77+ // is extended to Machine1.
78+ for (Machine1 value : list ) {
79+ System .out .println (value );
80+ value .start ();
81+ value .stop ();
82+
83+ // Cannot use the snap method from the Camera class
84+ // as it is not available in Machine1 class.
85+ // The following will throw an error.
86+ // value.snap();
87+ }
88+ }
89+
3090 public static void main (String [] args ) {
3191 ArrayList <Machine1 > list1 = new ArrayList <Machine1 >();
3292
@@ -37,9 +97,56 @@ public static void main(String[] args) {
3797
3898 list2 .add (new Camera ());
3999 list2 .add (new Camera ());
100+
101+ ArrayList <Car > list3 = new ArrayList <Car >();
102+
103+ list3 .add (new Car ());
104+ list3 .add (new Car ());
105+
106+ ArrayList <String > list4 = new ArrayList <String >();
107+
108+ list4 .add ("Dog" );
109+ list4 .add ("Cat" );
110+ list4 .add ("Horse" );
111+
112+ ArrayList <Bus > list5 = new ArrayList <Bus >();
113+
114+ list5 .add (new Bus ());
115+ list5 .add (new Bus ());
40116
41117 showList (list1 );
42118 showList (list2 );
43- }
119+ showList (list3 );
120+ showList (list4 );
44121
45- }
122+ System .out .println ("*************************" );
123+
124+ showListExtends (list1 );
125+ System .out .println ();
126+
127+ // Camera does not have stop method and hence
128+ // it reaches out to the stop method in the
129+ // parent class - Machine1
130+
131+ showListExtends (list2 );
132+ System .out .println ();
133+
134+ // Since car class does not have start / stop
135+ // method, it will use the start /stop method of
136+ // the parent class i.e. Machine1
137+
138+ showListExtends (list3 );
139+ System .out .println ();
140+
141+ // Cannot use object list4 as with showListExtends method as
142+ // it extends to Machine1 class. It will throw an error
143+ // if you try to use it:
144+ // showListExtends(list4);
145+
146+ // Since Bus class has stop / start method, it will
147+ // use it's own methods and will override the methods
148+ // provided by the parent class - Machine1
149+ showListExtends (list5 );
150+ System .out .println ("*************************" );
151+ }
152+ }
0 commit comments