Interface in Java with Example - Multiple Inheritance using Interface

An interface in java is syntactically similar to classes, but they do not have instance variable and their method are declared without any body. The methods declared inside an interface are by default abstract(only method signature, not implementation) and the variables declared inside an interface are public, static, and final by default. The interface in java is a mechanism to achieve abstraction. Once an interface is defined, any number of classes can implement this interface. Also, one class can implement any number of interfaces.



Why interface in java is used.

1. An interface in Java can be used to achieve total abstraction(100 % abstraction).
2. As we know, Java does not support multiple inheritance in the case of class but using the interface we can achieve multiple inheritance in java.
3. It can also be used to achieve loose coupling.

interface in java


How to define an Interface.

An Interface is defined much like a class. It can have methods and variables like class. But in Interface, all the methods are by default abstract and all the variables are by default public static, and final. This is the general form of an interface:

interface name{
  return-type method-name1(parameter-list);
  return-type method-name2(parameter-list);
  type varname1 = value;
  type varname2 = value;
  //.....
  return-type method-nameN(parameter-list);
  type varnameN = value;
}




How to implement an Interface.

Once an interface has been defined, one or more classes can implement that interface. To implement an interface, include the implements clause in a class definition, and create the methods defined by the interface. The general form of a class that includes the implements clauses look like this:

class classname implements interface-name{
    // class body
}
To implement an interface, a class must create a complete set of methods defined by the interface. However, each class is free to determine the details of its own implementation. If a class implements more than one interface, then the interfaces are separated with a comma.



Example of Interface in Java:

Let's understand the interface with the help of a real-world example of banks like PNB, SBI, OBC, etc. they have common functionality such as rate of interest. So we can make an interface and put all the common functionality inside this interface, and lets PNB, SBI, and OBC, etc implement these functionalities in their own class in their own way.

interface Bank{
	float rateOfInterest();
}
class PNB implements Bank{
	public float rateOfInterest(){
		return 9.5f;
	}
}
class SBI implements Bank{
	public float rateOfInterest(){
		return 8.5f;
	}
}
class OBC implements Bank{
	public float rateOfInterest(){
		return 7.5f;
	}
}
class InterfaceExample{
	public static void main(String args[]){
		Bank b = new PNB();
		System.out.println("Rate of interest of PNB is: " +b.rateOfInterest());
	}
}

Output:
Rate of interest of PNB is: 9.5


Multiple Inheritance by using the interface in java:

As we know, we cannot achieve multiple inheritance in the case of class because of the ambiguity problem. but using the interface we can achieve multiple inheritance in java. Let's see the example.




Java program for multiple inheritance using interface:


interface InterfaceOne{
	public void display();
}
interface InterfaceTwo{
	public void display();
}
class Main implements InterfaceOne,InterfaceTwo{
	public void display(){
		System.out.println("Display() method implementation");
	}
	public static void main(String args[]){
		Main obj = new Main();
		obj.display();
	}
}

Output:

Display() method implementation



Interface and Inheritance:

As we know, A class can implement an interface but an interface cannot implement another interface. One interface can extend another interface. One interface can extend another interface by use of extends keyword. The syntax is the same as for inheriting the classes. When a class implements an interface that inherits another interface, it must provide the implementation for all methods defined within the interface. Let's see the example:

interface InterfaceOne{
	public void display();
}
interface InterfaceTwo extends InterfaceOne{
	public void show();
}
class Main implements InterfaceTwo{
	public void display(){
		System.out.println("Display() method implementation.");
	}
	public void show(){
		System.out.println("Show() method implementation.");
	}
	public static void main(String args[]){
		Main obj = new Main();
		obj.display();
		obj.show();
	}
}

Output:

Display() method implementation.
Show() method implementation.



You may also like these posts:

1. Access Modifiers in Java with example.
2. Packages in Java with example.
3. Final Class, Final Variable, Final Method in Java.
4. What is the use of a super keyword in Java?
5. What is the need for this keyword in Java?
6. Java OOPs Concepts with example.
7. Types of operators in java with example.
8. Java Control statement with example.
9. Java Collection framework complete tutorial.
10. Top 100 Java interview question and answer.
Interface in Java with Example - Multiple Inheritance using Interface Interface in Java with Example - Multiple Inheritance using Interface Reviewed by Prashant Srivastava on December 27, 2019 Rating: 5

No comments:

Powered by Blogger.