Adding java 8 default examples
This commit is contained in:
21
java-8/java-8-default/MyCar.java
Normal file
21
java-8/java-8-default/MyCar.java
Normal file
@@ -0,0 +1,21 @@
|
||||
interface Car {
|
||||
default void printWheelCount() {
|
||||
System.out.println("4");
|
||||
}
|
||||
|
||||
void printColor();
|
||||
}
|
||||
|
||||
public class MyCar implements Car {
|
||||
|
||||
public static void main(String args[]) {
|
||||
MyCar myCar = new MyCar();
|
||||
myCar.printColor();
|
||||
myCar.printWheelCount();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void printColor() {
|
||||
System.out.println("Red");
|
||||
}
|
||||
}
|
||||
25
java-8/java-8-default/MyVehicle.java
Normal file
25
java-8/java-8-default/MyVehicle.java
Normal file
@@ -0,0 +1,25 @@
|
||||
interface MyCar {
|
||||
default public String getColor() {
|
||||
return "red";
|
||||
}
|
||||
}
|
||||
|
||||
interface MyBike {
|
||||
default public String getColor() {
|
||||
return "blue";
|
||||
}
|
||||
}
|
||||
|
||||
public class MyVehicle implements MyCar, MyBike {
|
||||
|
||||
public static void main(String args[]) {
|
||||
MyVehicle vehicle = new MyVehicle();
|
||||
System.out.println(vehicle.getColor());
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String getColor() {
|
||||
return MyCar.super.getColor();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
//Extending an interface that contains a default method
|
||||
//Example : Redeclare the 'default' method
|
||||
|
||||
interface FirstInterface {
|
||||
default public String getName() {
|
||||
return "Inside first interface";
|
||||
}
|
||||
}
|
||||
|
||||
interface SecondInterface extends FirstInterface {
|
||||
public String getName();
|
||||
}
|
||||
|
||||
public class MainClass implements SecondInterface {
|
||||
public static void main(String args[]) {
|
||||
MainClass mainClass = new MainClass();
|
||||
System.out.println(mainClass.getName());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Inside MainClass";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
//Extending an interface that contains a default method
|
||||
//Example : Redefine the 'default' method
|
||||
|
||||
interface FirstInterface {
|
||||
default public String getName() {
|
||||
return "Inside first interface";
|
||||
}
|
||||
}
|
||||
|
||||
interface SecondInterface extends FirstInterface {
|
||||
default public String getName() {
|
||||
return "Inside second interface";
|
||||
}
|
||||
}
|
||||
|
||||
public class MainClass implements SecondInterface {
|
||||
public static void main(String args[]) {
|
||||
MainClass mainClass = new MainClass();
|
||||
System.out.println(mainClass.getName());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
//Extending an interface that contains a default method
|
||||
//Example : Don't mention anything about the 'default' method in the new interface.
|
||||
|
||||
interface FirstInterface {
|
||||
default public String getName() {
|
||||
return "Inside first interface";
|
||||
}
|
||||
}
|
||||
|
||||
interface SecondInterface extends FirstInterface {
|
||||
}
|
||||
|
||||
class MainClass implements SecondInterface {
|
||||
public static void main(String args[]) {
|
||||
MainClass mainClass = new MainClass();
|
||||
System.out.println(mainClass.getName());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user