-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathAnonymousInnerClasses.java
72 lines (61 loc) · 2.06 KB
/
AnonymousInnerClasses.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
interface MyInterface {
void interfaceFun();
}
abstract class MyAbstractClass {
abstract void abstractFun();
void printMessage() {
System.out.println("I am inherited from My Abstract Class");
}
}
class NormalClass {
void normalFun() {
System.out.println("I am inherited from Normal Class");
}
void printMessage() {
System.out.println("I am inherited from Normal Class");
}
}
class ImplementingInterface implements MyInterface {
public void interfaceFun() {
System.out.println("Concrete implementation for interface method via implementing the interface");
}
}
public class AnonymousInnerClasses {
public static void main(String[] args) {
MyInterface obj = new ImplementingInterface();
obj.interfaceFun();
MyInterface obj2 = new MyInterface() {
@Override
public void interfaceFun() {
System.out.println("Concrete implementation for interface method through anonymous inner class");
ownMethod();
}
private static void ownMethod() {
System.out.println("A message from anon class's own method");
}
}; // Syntactical Sugar
obj2.interfaceFun();
System.out.println();
MyAbstractClass obj3 = new MyAbstractClass() {
@Override
public void abstractFun() {
System.out.println("Concrete implementation for abstract class method through anonymous inner class");
ownMethod();
}
private static void ownMethod() {
System.out.println("A message from anon class's own method");
}
}; // Syntactical Sugar
obj3.abstractFun();
obj3.printMessage();
System.out.println();
NormalClass obj4 = new NormalClass() {
@Override
void normalFun() {
System.out.println("Overridden implementation by anon class");
}
};
obj4.normalFun();
obj4.printMessage();
}
}