Java Design Pattern
Introduction to Java 10
Introduction to Java 11
Introduction to Java 12

Implementing Interfaces

Interfaces are used as super classes whose properties are inherited by a class. Thats why it is necessary to create a class that inherits the given interface. This is achieved as follows

Example:
class ClassName implements InterfaceName
{
	Body of class
}
//The more general form of implementation is
												
class ClassName extends superclass implements Interface1,
Interface2........
{
	body of className
}

Here it represents that a class can extend another class while implementing interfaces
Let us see a program to understand this concept


Example:
interface Print
{  
	void display();
} 										  
class Printing implements Print
{
	public void display()
	{
		System.out.println("JavaRace");
	}
}
class InterfaceDemo
{
	public static void main(String args[])
	{
		Printing ob = new Printing();  
		ob.display();
	}												
}
Output

JavaRace


Multiple inheritance in Java by interface:

If a class implements multiple interfaces, or an interface extends multiple interfaces i.e. known as multiple inheritance.
Img Insert Here.
Let's see the following program:

Example:
interface Test1
{ 	
	void display();  								
} 												  
interface Test2 
{   			
	void show();   				
}   		  
class Simple implements Test1, Test2 
{  									  
	public void display(); 
	{ 		
		System.out.println("JavaRace");		
	} 											
	public void show()
	{ 													
		System.out.println("Silan Software");											
	}											 
}
class MultipleDemo 
{ 
	public static void main(String args[])
	{ 
		Simple s = new Simple();
		s.display(); 
		s.show();   
	}
}
Output

JavaRace
Silan Software

Note:

Multiple inheritance is not supported in case of class. But it is supported in case of interface because there is no ambiguity as implementation is provided by the implementation class.

About the Author



Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.


We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc






 PreviousNext