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!

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to use Form Recognizer API

Examples of Putting ECK+PSO to Work

OpenMetadata 0.9.0 Release

How to Free Up RAM on a Windows Computer

Creating forms with django.

Master & Dynamic: A fictitious mobile app for my MW07 GO

Case study of Applitools or not only Cypress cross-browser testing

Getting started with Arduino

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
Code Factory

Code Factory

More from Medium

Naming Conventions in Java

Java OOPS Concepts

Java #4 | OOP Object Oriented Programming (1)

Understanding Abstract Window Toolkit in Java