What is the interface in java?
Interface in java is one of OOP’s concepts. The interface in java is a mechanism to achieve 100% abstraction.
The interface provides achieve 100% abstraction because it uses only abstract methods (not method body).
An interface can be used only abstract methods (without body), before java 8.
After java 8 we can have default and static methods in an interface. In java 9 we have private methods also.
The interface is declared by using the interface keyword, and all the fields are public, static, and final by default.
Why we are using the Java interface?.
There are many reasons to use Java interface but there are mainly three reasons.
- The interface provides 100% abstraction.
- Interface we can achieve multiple inheritance functionality in java.
- Interface provides loose coupling.
Java Interface program Example
interface printer {
void print();
}
class Book implements printer {
public void print() {
System.out.println("Book Printing");
}
public static void main(String args[]) {
Book obj = new Book();
obj.print();
}
}
Output:
Book Printing
Java Interface program another Example
interface Shape {
void draw();
}
class Rectangle implements Shape {
public void draw() {
System.out.println("drawing rectangle");
}
}
class Circle implements Shape {
public void draw() {
System.out.println("drawing circle");
}
}
class Test {
public static void main(String args[]) {
Shape d = new Circle();
d.draw();
Shape d = new Rectangle();
d.draw();
}
}
Output:
drawing circle
drawing rectangle
In the above example, we are creating an interface Shape, and it has only one method that is abstract method draw() (without body).
Shape interface implementation is provided by Rectangle and Circle classes. In this scenario, the implementation part is always hidden by the user, who uses the interface.
In this program Interface declaration by one user, and its implementation provided by other users, based on their requirement.
Multiple inheritances not supported by java class, but it is supported by the Java interface.
How Multiple inheritances supported by Java interface?
Multiple inheritances mean if a class implements multiple interfaces, or an interface extends multiple interfaces, it is known as multiple inheritances in java.
Class multiple interfaces Example.
interface Shape {
void draw();
}
interface Print {
void print();
}
class Example implements Shape, Print {
public void print() {
System.out.println("Printing");
}
public void draw() {
System.out.println("Drawing");
}
public static void main(String args[]) {
Example obj = new Example();
obj.print();
obj.draw();
}
}
Output:
Printing
Drawing
In this example, the class Example implements multiple interface Shape, Print.
Interface extends multiple interfaces for Example.
interface Print {
public void printing();
}
interface Draw {
public void drawing();
}
interface Painter extends Print, Draw {
public void printing();
}
class Example implements Painter {
public void printing() {
System.out.println("printing\n");
}
public void drawing() {
System.out.println("drawing\n");
}
public void printingMain() {
System.out.println("printing main");
}
}
public class Test {
public static void main(String[] args) {
Example ex = new Example();
ex.printing();
ex.printing();
ex.printingMain();
}
}
Output:
printing
printingprinting main
Multiple inheritances are not supported by class in java, but it is possible by an interface, why?
Multiple inheritances are not supported in the case of class, because of ambiguity but it is supported by interface because we are not getting any ambiguity.
For more details of Multiple inheritance Click.
Read more topics related to java
- Polymorphism in java
- Encapsulation in Java with Examples
- synchronization in java
- Exception in java
- Inner class in java
- Different between String and String Buffer
- Top 5 string programming interview questions in java
- String in java
- Object-Oriented Programming (OOPS) Concept in java
Hope this was helpful for you. If you have any questions please feel free to leave a comment. Thank you for reading.