-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathch_57_default_method.java
More file actions
38 lines (29 loc) · 1003 Bytes
/
Copy pathch_57_default_method.java
File metadata and controls
38 lines (29 loc) · 1003 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
28
29
30
31
32
33
34
35
36
37
38
interface MyCamera {
private void greet() {
System.out.println("\nPrivate method accessed by calling it with other methods from that interface");
}
default void record4KVideo() {
greet();
System.out.println("\nDefault method in Interface can have definition in it");
}
}
class MySmartPhone implements MyCamera {
public void show() {
System.out.println("This is MySmartPhone");
}
}
public class ch_57_default_method {
public static void main(String[] args) {
MySmartPhone ms = new MySmartPhone();
ms.record4KVideo();
// ms.greet(); // Throws an error!
}
}
/*
* default methods help to add methods with definitions inside interface.
* private methods inside interface can only be used by other methods of that
interface. The use of private methods, is to act as helper methods for
default methods.
* If default method has 1 more definition in its derived class, then it will be
overridden.
*/