Skip to content

Commit d301ffb

Browse files
author
Rocky Bhai
committed
Uploaded on 1st-Feb - 2nd commit.
1 parent ef9713a commit d301ffb

File tree

2 files changed

+169
-2
lines changed

2 files changed

+169
-2
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package sample;
2+
3+
// This example is to test the anonymous classes
4+
5+
class Books {
6+
public void name() {
7+
System.out.println("Printing book name.");
8+
}
9+
public void author() {
10+
System.out.println("Calling author inside Book class.");
11+
}
12+
}
13+
14+
interface Paper {
15+
public void pages();
16+
public void thick();
17+
}
18+
19+
public class AnonymousClass {
20+
21+
public static void main(String[] args) {
22+
23+
Books b1 = new Books() {
24+
// Creating an override method while creating an object
25+
26+
@Override
27+
public void name() {
28+
System.out.println("Printing book name inside main.");
29+
}
30+
public void author() {
31+
System.out.println("Book has been written by a famous author.");
32+
}
33+
public void pages() {
34+
System.out.println("Printing the number of pages inside main.");
35+
}
36+
};
37+
38+
b1.name();
39+
b1.author();
40+
41+
// The following will produce an error as the pages
42+
// method is not existing inside Books class.
43+
44+
// b1.pages();
45+
46+
Paper p1 = new Paper() {
47+
@Override
48+
public void pages() {
49+
System.out.println("Page size is A4");
50+
}
51+
public void thick() {
52+
System.out.println("Page is thick");
53+
}
54+
};
55+
56+
p1.pages();
57+
p1.thick();
58+
}
59+
60+
}

javabasic/sample/GenericsWildcard.java

Lines changed: 109 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

1521
class 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

2263
public 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

Comments
 (0)