Multiple Inheritance Using Interface Example Java Program


Definition

Inheritance is when an object or class is based on another object or class, using the same implementation specifying implementation to maintain the same behavior. It is a mechanism for code reuse and to allow independent extensions of the original software via public classes and interfaces. The relationships of objects or classes through inheritance give rise to a hierarchy. Multiple Inheritance allows a class to have more than one super class and to inherit features from all parent class. it is achieved using interface.

Syntax

public interface A{
	//Do Something
}
public interface B extends A{
	//Do Something
}
public interface C extends A{
	//Do Something
}

Multiple Inheritance Using Interface Example Program

 
interface vehicleone{
	int  speed=90;
	public void distance();
}

interface vehicletwo{
	int distance=100;
	public void speed();
}

class Vehicle  implements vehicleone,vehicletwo{
	public void distance(){
		int  distance=speed*100; 
		System.out.println("distance travelled is "+distance);
	}
	public void speed(){
		int speed=distance/100;
	}
}

class MultipleInheritanceUsingInterface{
	public static void main(String args[]){
		System.out.println("Vehicle");
		obj.distance();
		obj.speed();
	}
}

Sample Output

Output is:
distance travelled is 9000