Default Method in Java 8 | Code Factory

package com.codeFactory;/**
* @author code.factory
*
*/
interface TestInterface {
/*abstract method*/
public void add(int a, int b);

/*default method*/
default void show() {
System.out.println("Code Factory!");
}
}
public class TestClass implements TestInterface {@Override
public void add(int a, int b) {
System.out.println(a + b);
}
public static void main(String... args) {
TestClass testClass = new TestClass();
testClass.add(5, 10);

testClass.show();
}
}
15
Code Factory!

Static Method :

package com.codeFactory;/**
* @author code.factory
*
*/
interface TestInterface {
/*abstract method*/
public void add(int a, int b);

/*static method*/
static void show() {
System.out.println("Code Factory!");
}
}
public class TestClass implements TestInterface {@Override
public void add(int a, int b) {
System.out.println(a + b);
}
public static void main(String... args) {
TestClass testClass = new TestClass();
testClass.add(5, 10);

TestInterface.show();
}
}
15
Code Factory!

Default Methods and Multiple Inheritance :

package com.codeFactory;/**
* @author code.factory
*
*/
interface TestInterface1 {
/*default method*/
default void show() {
System.out.println("Code Factory 1!");
}
}
interface TestInterface2 {/*default method*/
default void show() {
System.out.println("Code Factory 2!");
}
}
public class TestClass implements TestInterface1, TestInterface2 {public static void main(String... args) {
TestClass t = new TestClass();
t.show();
}

public void show() {
TestInterface1.super.show();
TestInterface2.super.show();
}
}
Code Factory 1!
Code Factory 2!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store