-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefaultMethods.java
More file actions
40 lines (31 loc) · 842 Bytes
/
Copy pathDefaultMethods.java
File metadata and controls
40 lines (31 loc) · 842 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
39
40
package java8_features;
/**
* In java 8 interfaces can have default methods, methods with code.
*/
public class DefaultMethods {
public static void main(String[] args) {
Car car = new Car();
car.description();
car.startEngine();
}
}
class Car implements Vehicle, FourWheeler {
// We have to override the method description because it is inherited from two different interfaces.
@Override
public void description() {
System.out.println("I'm a four wheeled vehicle!!");
}
}
interface Vehicle {
default void startEngine() {
System.out.println("Bruuuuum!!!");
}
default void description() {
System.out.println("I'm a vehicle");
}
}
interface FourWheeler {
default void description() {
System.out.println("I'm a four wheeler!");
}
}