Interface in Java. In English

                                  Interface


In Java, an interface is a class's blueprint. It features abstract methods and static constants. Java's interface is a tool for achieving abstraction. The Java interface can only include abstract methods; method bodies are not allowed. In Java, it's utilized to accomplish multiple inheritance and abstraction. Stated differently, it is possible for interfaces to contain abstract variables and methods. There can be no method body for it. 

The IS-A relationship is also represented via the Java Interface.
Similar to the abstract class, it cannot be instantiated. An interface may contain both static and default methods. An interface may contain private methods.

Utilizing an interface is primarily done for three reasons.

  1. It is used to achieve abstraction.
  2. By interface, we can support the functionality of multiple inheritance.
  3. It can be used to achieve loose coupling.

The interface keyword is used to declare an interface. It offers complete abstraction, which entails that every method in an interface is declared with an empty body and that every field is by default public, static, and final. Every method specified in an interface must be implemented by a class that implements it.

Syntax for Java Interfaces: -

  1. interface {  
  2.       // declare constant fields  
  3.     // declare methods that abstract   
  4.     // by default.  
  5. }  

Java Interfaces' Uses

  • The goal of using it is complete abstraction.
  • Java does not enable multiple inheritances for classes; however, multiple inheritances can be achieved through the use of interfaces.
  • While a class can implement an endless number of interfaces, it can only extend one class.
  • To accomplish lax coupling, it is also employed.
  • Abstraction is implemented via interfaces. 

The relationship between classes and interfaces
A class extends another class, an interface extends another interface, but a class implements an interface, as illustrated in the image below.


Difference Between Class and Interface


Java interface example

  1. interface A{  
  2. void print();  
  3. }  
  4. class B implements A{  
  5. public void print(){System.out.println("Hello Java");}  
  6.   
  7. public static void main(String [] args){  
  8. B b = new B();  
  9. b.print();  
  10.  }  
  11. }  
Output - Hello Java

Multiple Inheritance in Java Using Interface
If a class implements multiple interfaces, or an interface extends multiple interfaces, it is known as multiple inheritance. Java classes cannot be used to implement the OOPs concept of multiple inheritance. In Java, however, we can leverage multiple inheritances by utilizing an interface. 

                                 Multiple inheritance in using interface

Example: -
  1. interface A{  
  2. void print();  
  3. }  
  4. interface B{  
  5. void show();  
  6. }  
  7. class Test implements A,B{  
  8. public void print(){System.out.print("Hello");}  
  9. public void show(){System.out.println(" Welcome Java");}  
  10.   
  11. public static void main(String args[]){  
  12. Test t = new Test();  
  13. t.print();  
  14. t.show();  
  15.  }  
  16. }  
Output - Hello Welcome Java

Important Points in Java Interfaces
  • Although an interface cannot be instantiated, we are still able to make a reference to it that points to the object of the class that implements it.
  • A class may support several interfaces.
  • An interface has the ability to extend to many interfaces, or other interfaces.
  • Every method in the interface must be implemented by the class that implements it.
  • Every technique is abstract and open to the public. Additionally, every field is static, public, and final.
  • Multiple inheritances are accomplished with its help.
  • To accomplish loose coupling, it is employed.
  • Since variables are set to public static final by default, it is not allowed to specify instance variables inside the interface.
  • Constructors are not permitted within the Interface.
  • The main method inside the interface is prohibited.
  • It is not feasible to declare static, final, or private methods inside of an interface.
Advantages of Interfaces in Java
  1. We can achieve the implementation's security without worrying about the implementation itself.
  2. multiple inheritance is not permitted in Java; however, you can take advantage of this limitation by implementing numerous interfaces.

Comments

Popular posts from this blog

Lambda expression in Java. In English

Loops in Java. In English

Conditions in java. In English