-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDefaultMethodDemo.java
More file actions
27 lines (19 loc) · 890 Bytes
/
DefaultMethodDemo.java
File metadata and controls
27 lines (19 loc) · 890 Bytes
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
package chapter9;
//Use default method..
public class DefaultMethodDemo {
public static void main(String[] args) {
MyIFImp obj = new MyIFImp();
//Can call getNumber() because it is explicitly implemented in MyIFImp
System.out.println(obj.getNumber());
//Can also call getString() because of default implementation
System.out.println(obj.getString());
MyIFImp2 obj2 = new MyIFImp2();
//Can call getNumber() because it is explicitly implemented in MyIFImp2
System.out.println(obj2.getNumber());
//Can also call getString() it is implemented in MyIFImp2
System.out.println(obj2.getString());
//Since getDefaultNumber() method is static we had chance to use it by its interface name
int defNum = MyIF2.getDefaultNumber();
System.out.println("Default Number " + defNum);
}
}