What is Interface in Java?
Interface: An Interface has 3 meanings.
• It is an intermediate between the service and the consumer.
• It is also called a 100% abstract class.
• It is also called a rules repository or coding contract.
Programmatically, we create an interface by using a keyword interface. All the methods in an interface are automatically public and abstract. The interface cannot have Constructors and instance variables. Why use Java interface? There are mainly three reasons to use an interface.
They are given below. o It is used to achieve abstraction. o By interface, we can support the functionality of multiple inheritances. It can be used to achieve loose coupling. How to declare an interface?
An interface is declared by using the interface keyword. It provides total abstraction; which means all the methods in an interface are declared with the empty body, and all the fields are public, static and final by default. A class that implements an interface must implement all the methods declared in the interface.
Syntax: --
interface
Example:
public interface RegulatorInf{
void increaseSpeed();
void reduceSpeed();
}
public interface Iswitch{
void switchOn();
void switchOff();
}
Comments
Post a Comment