Skip to content

Commit 1bf925c

Browse files
committed
default method Multiple Inheritance
1 parent 88cda69 commit 1bf925c

File tree

5 files changed

+73
-0
lines changed

5 files changed

+73
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.learn.defaults;
2+
3+
/**
4+
* This Client implements Interface1, Interface2 and Interface3
5+
*
6+
* Order of resolving the method calls during runtime.
7+
* 1 -> Class that implements the interface.
8+
* 2 -> the sub interface that extends the interface.
9+
*
10+
*/
11+
public class Client123 implements Interface1, Interface2, Interface3{
12+
13+
/**
14+
* The class that overrides the default method takes precedence
15+
* over any other implementation that we have in other interfaces.
16+
*/
17+
public void methodA() {
18+
System.out.println("Inside methodA() from Client123");
19+
}
20+
21+
public static void main(String[] args) {
22+
Client123 client123 = new Client123();
23+
24+
/**
25+
* This concept is called multiple inheritance.
26+
* We are inheriting multiple behaviors from each and every interfaces.
27+
* This was never possible before Java8
28+
*/
29+
client123.methodA();
30+
client123.methodB();
31+
client123.methodC();
32+
33+
34+
/**
35+
* Let's add some more complexity
36+
* by extending Interface1 in Interface2
37+
* by extending Interface2 in Interface3
38+
*/
39+
client123.methodA(); // resolve to the child implementations.
40+
}
41+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.learn.defaults;
2+
3+
public interface Interface1 {
4+
5+
default void methodA() {
6+
System.out.println("Inside methodA() from Interface1");
7+
}
8+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.learn.defaults;
2+
3+
public interface Interface2 extends Interface1{
4+
5+
default void methodB() {
6+
System.out.println("Inside methodB() from Interface2");
7+
}
8+
9+
@Override
10+
default void methodA() {
11+
System.out.println("Inside methodA() from Interface2");
12+
}
13+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.learn.defaults;
2+
3+
public interface Interface3 extends Interface2{
4+
5+
default void methodC() {
6+
System.out.println("Inside methodC()");
7+
}
8+
9+
10+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ This repository contains the basic & advance level examples related to Java
6767
* [sort using Comparator](Modern-Java-Examples/src/com/learn/defaults/DefaultsMethodsExample2.java)
6868
* [Comparator: nullsFirst and nullsLast](Modern-Java-Examples/src/com/learn/defaults/DefaultsMethodsExample2.java)
6969
* [default and static methods in Interface](Modern-Java-Examples/src/com/learn/defaults/Multiplier.java)
70+
* [default method Multiple Inheritance](Modern-Java-Examples/src/com/learn/defaults/Client123.java)
7071

7172
<hr />
7273

0 commit comments

Comments
 (0)