11 July 2023

What is an Interface In Java - learngreen.net

 In Java, an interface is a reference type that defines a set of summary methods. It serves as a contract or blueprint for the class to follow. It specifies which methods the class have to implement except specifying how to implement them.


What is an Interface In Java?


Here are some key points about Java interfaces:-

1.  An interface is described by way of the key-word "interface" followed through the title of the interface. It can additionally contain general fields and default methods.

2. Abstract Methods:- An interface can declare summary methods that each and every class implementing the interface must implement. These methods have no body and are public and summary by using default. Any class that implements an interface must supply an implementation of all summary methods.  

3 Implementation of multiple interfaces:- A Java type can put into effect multiple interfaces. This lets in a class to inherit behavior from a couple of sources and operate a couple of inheritance-like operations.

4. Interface Inheritance:- Interfaces can additionally extend different interfaces the usage of the "extends" keyword. This approves growing a hierarchy of interfaces the place the child interface inherits methods and constants from the parent interface.

5. Default Methods:- Since Java 8, interfaces can have default methods. A default technique is a method that has a default implementation inside the interface itself. These techniques can be overridden by means of implementation classes, but they supply a default implementation if now not overridden through the implementation class. 

6. Persistent Fields:- Interfaces can declare persistent fields that are implicitly public, static, and final. These fields can be accessed the usage of the UI name and the discipline name.

7. Application isolation:- Interfaces assist isolate the specification or contract of the application. By programming the user interface, you can write flexible and extensible code that works in distinct implementations of the person interface.

Interfaces are extensively used in Java to outline conventions and acquire type polymorphism. They enable for loose coupling, code reusability, and abstraction, allowing instructions to put into effect a couple of interfaces and outline behavior barring implementation information.




 Syntax of Interface

 interface {

 // constant fields

 // Abstract methods

 // Default method (Java 8 onwards)

 }

 To declare an interface, use the interface keyword. It is used to provide complete abstraction. That capacity all the methods in an interface are declared with an empty body and are public and all fields are public, static, and closing by default. A type that implements an interface must implement all the methods declared in the interface. To implement interface use implements keyword.

Importance of Interface:-

It is used to achieve complete abstraction.  Since java does not support multiple inheritance for a class, multiple inheritance can be achieved using an interface. Each class can extend only one class, but each class  can  implement an infinite number of interfaces. It is also used to secure a loose joint. Interfaces are used to implement abstraction. So the question arises, why use interfaces when we have abstract classes? This is because abstract classes can contain non-final variables, while interface variables  are final, public and static.

 Object of interface could not be created but we can create reference variable of interface type. By assigning this reference variable to object of class which implements interface

A class has the potential to put in force more than one interfaces simultaneously. This potential that a single type can declare that it adheres to the specs of more than one interfaces and furnish implementations for all the techniques described inside every interface. By imposing more than one interfaces, a type can inherit and make use of the behaviors and contracts distinctive by each interface independently.

Interfaces have the ability to prolong different interfaces, which approves them to inherit strategies from the prolonged interfaces. This capacity that an interface can declare additional strategies in addition to the techniques inherited from the prolonged interfaces. By extending interfaces, new functionality and approach contracts can be added, presenting greater precise behaviors and requirements for instructions that put in force these interfaces.

When a class implements an interface, it is required to provide implementations for all the strategies that are described inside the interface. This capacity that the class need to define the unique functionality and behavior for every method declared in the interface. By imposing the interface, the class is in reality making a promise to adhere to the contract designated with the aid of the interface and to furnish the integral code for every method. Failure to implement any of the interface methods in the category will end result in a compilation error.

In an interface, all methods are by default public and abstract. This capability that the strategies declared in an interface do now not have a body or implementation. They serve as a contract or a declaration of the methods that any type imposing the interface have to define. The accountability of supplying the proper implementation of these techniques lies with the classes that implement the interface. Therefore, when a class implements an interface, it need to supply the concrete implementation of all the abstract methods declared in the interface.  All methods in an interface are public and abstract, which means they do not have a body. All the fields in an interface are public, static, and final, making them constant values. Interfaces are beneficial for accomplishing multiple inheritances, enabling a class to inherit behaviors from more than one interfaces. Interfaces promote free coupling between classes, making it less complicated to change implementations barring affecting other components of the code. Interface cannot declare occasion variables because they are by default public, static, and final. Constructors are now not allowed inner an interface. The essential method cannot be declared inner an interface. 


interface First { //Default method that prints // "www.learngreen.net" default void Language() { System.out.println ("www.learngreen.net"); } } class programming implements First { public static void main(String[] args) { //Create an object of class programming programming skill = new programming(); //Call the Language() method skill.Language(); //Output-> www.learngreen.net } }

 



 interface MyClassOne {
 //Abstarct method declaration    
 public void information(); }
 class child implements MyClassOne {
  public void information()  {
 //Implementation of the
 // information() method 
 String name = "Shyam";
 int height = 6;
 System.out.println
 ("name "+ name);
 System.out.println
 (" height " +height+ " ft");
  }  }
 public class MyClass1 {
 public static void main(String[] args) {
 //Create an object of child class
 child object = new child();
 //Call the information() method       
 object.information();
//Output-> name shyam
//         height 6 ft      
    }
 }

 



No comments:

Post a Comment