Introduction
In this article, we will discuss the interface
in Java. As multiple inheritances in Java only achieve through Interface.
So we discuss Interface in Java.
What is Interface?
- In computing, the interface acts like a communicator between peripheral devices(monitor, keyboard) and computer system.
- The infield of computer science, an interface refers to an interactor between components.
- In computer any type of communication of computer system with the user we say it is an interface.
Interface in Java
An interface is a blueprint of a class. It is a java core part and a way to achieve data abstraction in Java along with the abstract class. Since multiple inheritances are not allowed in Java, the interface is the only way to implement multiple inheritances. At in basic level interface in java is a keyword but that time it is an object-oriented term to define contracts and abstraction, This contract is followed by any implementation of Interface in Java.
Key Point of Interface in JAVA
- You can fully abstract a class using keyword interface.
- While declaring an interface in a class there is no need for a keyword abstract.
- All methods in an interface are implicitly public.
- The interface is not a class defining the interface is similar to defining a class, but they are two different concepts. A class defines the attributes and behaviors of an object. An interface contains behaviors that a class implements.
- We have to define all the abstract methods of the interface in the class.
Declaring Interfaces
Using the Interface keyword to declare an interface.
Example
Define a simple interface
- interface Ex1
- {
- public static final int min=10;
- public abstract void print();
- }
Define another interface
- interface Ex2
- {
- public static final int min=5;
- public abstract void show();
- }
Implementing Interfaces
A class uses the implements keyword to implement an interface.
If a class implements more than one interface, they are separated with a comma.
The methods that implement an interface must be declared public (to be accessed by other members). Also, the type signature of the implementing method must match
exactly the type signature specified in the interface definition.
Sample Program




Join the conversation! Your thoughts help the community grow.