Multiple Inheritance support in Java 8

20/10/2016 Amit Gupta 401 Java

In this article, we will learn support of multiple inheritance in Java 8 with some examples.

As we know prior to Java 8, multiple inheritance was not supported in Java which means that a class cannot extend two other classes at a time. It is also known as Diamond Problem as shown below: 

Multiple Inheritance and Diamond Problem

For example:

/**
 * @author Amit Gupta
 *
 */
public class Dictionary {

	public void words(){
		
	}
	public void write(){
		
	}
}

/**
 * @author Amit Gupta
 *
 */
public class Language {
	
	public void write(){
		
	}
	
	public void read(){
		
	}
}
 
/**
 * @author Amit Gupta
 *
 */
public class English extends  Language,  Dictionary {

	@Override
	public void write() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void read() {
		// TODO Auto-generated method stub
		
	}
        @Override
        public void words(){
		
	}

}

As in Java 7 and lower versions, we cannot extend two different classes, we cannot achieve multiple inheritance in Java. An alternate way to achieve this was using interfaces as you can implement multiple interfaces in Java. 

In Java 8, we can create a method with body in an interface i.e. default method. Default method enables you to add new functionality to the interfaces and ensure backwards compatibility for existing classes which implement that interface.

For example:- 

/**
 * @author Amit Gupta
 *
 */
public interface Dictionary {

	public default void words(){
		
	}
	
	public default void write() {
		
	}
	
}


/**
 * @author Amit Gupta
 *
 */
public interface Language {
	
	public default void write(){
		
	}
	
	public default void read(){
		
	}
}

/**
 * @author Amit Gupta
 *
 */
public class English implements Language, Dictionary {

	@Override
	public void write() {
		
		
	}

	@Override
	public void read() {
		
		
	}

}

If you override method defined in both of the interfaces like in above example we have overridden write() method defined in both interfaces, Java 8 provides mechanism to invoke the method of desired interface using INTERFACE_TYPE.super.method_name() as shown below:

/**
 * @author Amit Gupta
 *
 */
public interface Dictionary {

	public default void words(){
		
	}
	
	public default void write() {
		System.out.println("Please let us know in which language the dictionary is to be created");
	}
	
}

/**
 * @author Amit Gupta
 *
 */
public interface Language {
	
	public default void write(){
		System.out.println("Default Language not identified");
	}
	
	public default void read(){
		
	}
}


/**
 * @author Amit Gupta
 *
 */
public class English implements Language, Dictionary {

	@Override
	public void write() {
		Language.super.write();
		Dictionary.super.write();
		System.out.println("Language selected is English");
		
	}

	@Override
	public void read() {
		System.out.println("You are reading English");
		
	}

	
	public static void main(String[] args) {
		English english = new English();
		english.read();
		english.write();
		
	}
}

In this way we were able to use multiple inheritance in Java 8. To learn more about Java 8 visit Java 8 features. In this article, we learnt support of multiple inheritance in Java 8 and its usage.

Article tagged as
Author
Author: Amit Gupta
Published On: 20/10/2016
Last revised On: 10/23/2016 11:15:08 AM
View all articles by Amit Gupta

Share this post

Comments

Comments
comments powered by Disqus

Navigation

Social Media