Templates by BIGtheme NET

Java 8 Default Methods

In this Article, I will show How to work with Java 8 Default method implementation
for Interface. This is one of the useful java 8 feature.

What is Default methods in Java 8?

Default methods enable us to add new functionalities to interfaces
without breaking the classes that implements that interface.

Prior to Java 8, Adding a new method to an interface means all concrete classes
must provide an implementation for it. To avoid this problem,
Java 8 introduces default methods in interfaces.

Methods like stream() and parallelStream() are new methods of List<T>
interface in Java 8.

Given is the declaration in Collection<T> interface,

default Stream<E> stream() {
       return StreamSupport.stream(spliterator(), false);
}

default Stream<E> parallelStream() {
	return StreamSupport.stream(spliterator(), true);
}

These methods are declared with default, If these methods are declared as normal methods
without default methods this code won’t compile.

You can apply these simple solution to your project as well.

We can declare the methods in interfaces as default and provide
some default implementation, then all concrete classes may or may not
implement these methods.

This is related to Lambda expressions :

One of the most useful advantage is,
Default methods are allowing an interface to stay a functional interface even if it has more than one method.
Here one method will be abstract method and all other methods will be default methods.

– Functional Interface is a function having SAM (Single abstract method).

DefaultMethodExample3.java

package com.devjavasource.java8;

public class DefaultMethodExample3 {

	public static void main(String[] args) {

		// Lambda Add function
		SimpleInterface1 obj = () -> System.out
				.println("Hello JAVA 8 Lambda example!\n");
		obj.method1();
	}

	public interface SimpleInterface1 {
		public abstract void method1();

		default public void fly() {
			System.out.println("This is Default implementation of fly ...");
		};
	}
}

Select and Run As -> Java Application.
Out Put :

Hello JAVA 8 Lambda example!

Simple maven project with java program to understand
usage of default methods,

DefaultMethodExample1.java

In this example fly() is default method, having default implementation
in Bird interface.

Hen is the class, not having any implementation to fly() method.

package com.devjavasource.java8;

public class DefaultMethodExample1 {

	public static void main(String[] args) {
		
		Hen aHen = new Hen();
		aHen.fly();
		
		Crow aCrow = new Crow();
		aCrow.fly();
	}	

	interface Bird {
		public void eat();
		public void drink();
		public void walk();
		default public void fly() { System.out.println("This is Default implementation of fly ..."); };
	}
	
	public static class Hen implements Bird{
		
		@Override
		public void eat() {
	        System.out.println("Hen Specific - eating");
	    }		
		
		@Override
	    public void drink() {
	        System.out.println("Hen Specific - drinking");
	    }
	    
		@Override
	    public void walk() {
	        System.out.println("Hen Specific - walking");
	    }
	}
	
	public static class Crow implements Bird{

		@Override
		public void eat() {
			System.out.println("Crow Specific - eating");
		}

		@Override
		public void drink() {
			System.out.println("Crow Specific - drinking");
		}

		@Override
		public void walk() {
			System.out.println("Crow Specific - walking");
		}
		
		@Override
		public void fly() {
			System.out.println("Crow Specific - flying");
		}
	}
}

Select and Run As -> Java Application.
Out Put :

This is Default implementation of fly ...
Crow Specific - flying

DefaultMethodExample2.java

package com.devjavasource.java8;

public class DefaultMethodExample2 {

	public static void main(String[] args) {
		
		A a = new Clazz();
		a.m1();
		
		B b = new Clazz();
		b.m1();
	}	

	interface A {
		default public void m1() { System.out.println("A interface : method m1 ..."); };
	}
	
	interface B {
		default public void m1() { System.out.println("A interface : method m1 ..."); };
	}
	
	public static class Clazz implements A, B{
		
		@Override
	    public void m1() {
	        // In case, we want to call A interface method m1 always...
			A.super.m1();
	    }
	}
}

Select and Run As -> Java Application.
Out Put :

A interface : method m1 ...
A interface : method m1 ...

You can download complete project, Here

Java8DefaultMethod

*** Venkat – Happy leaning ****