dcsimg
LinkedIn
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
May 12, 2021

Java: Implementing Interfaces with Default Methods

In the sample code below, you will find some samples code showing how to implement an interface with a default method using Java. Note that there are cases in which multiple interfaces will have the same default method. Feel free to explore those instances on your own and come back here for a future update.

In addition to implementing interface with default methods, you can also try commenting out the the print method in the Impl class and observe the difference in behavior of the call flow to the print method.

*/

public class DefaultMethodInterfaceImpl implements DefaultMethodInterface {

	public static void main(String args[])
	{
		DefaultMethodInterfaceImpl defaultMethodInterfaceImpl = new DefaultMethodInterfaceImpl();
		defaultMethodInterfaceImpl.print();
	}

	public void print() {
		System.out.println("In print of Impl.");
		DefaultMethodInterface.super.print();
	}
}

/*

Output:

$java DefaultMethodInterfaceImpl
In print of Impl.
I am the default method in the interface!
MS Sridhar
 
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date