-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterfaces.java
More file actions
64 lines (51 loc) · 1.63 KB
/
Interfaces.java
File metadata and controls
64 lines (51 loc) · 1.63 KB
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
// Interfaces in Java Don't Have Memory Allocated Like Objects
interface A{
//public abstract - by default
void show();
//all variables in interface is final and static by default!
// int a; // a static var must be declared a value for sure!
int a = 10; // works! static and final by default!
//to get this value, use interface name.var name
//since these variables are static!
}
abstract class B{
public abstract void show2();
}
//implement an interface?
class C implements A{
public void show(){
System.out.println("Implementing an interface A through seperate class C");
}
}
//implement an abstract class?
//if this class does not implement all abstract func, then it becomes abstract class!
class D extends B{
public void show2(){
System.out.println("Implementing an abstract class B through seperate class D");
}
}
public class Interfaces{
public static void main(String args[]){
//power and elegance of anonymous classes!
A objA; //create ref for interface! works!
objA = new A(){ // obj creation works only with inline class!
public void show(){
System.out.println("Interface A implementation for show using anonymous inline class!");
}
};
objA.show();
B objB; //create ref for abstract class! works!
objB = new B(){ //obj creation works only with inline class!
public void show2(){
System.out.println("Abstract class B implementation for show2 using anonymous inline class!");
}
};
objB.show2();
C objC = new C();
objC.show();
D objD = new D();
objD.show2();
System.out.println(A.a); //interfaceName.varName
// A.a = 11; //error: cannot assign a value to static final variable a
}
}