Java Online Training In Andhra Pradesh and Telangana
Opening Hours :7AM to 9PM
Interface is similar to class.It is used to achieve the multiple inheritance in java language.
Interface is replacement of abstract class. Syntax:
list of public static final variables;
list of abstract methods();
Every interface can be identify with interface keyword and represented by user defined name.
Defination of interface:
Interface is similar to class,Which is collection of public static final variables and abstract methods.
The main advantege with interface is common reusability that means these methods can be reused many number of classes with separate implementation part.
Interface always contains variable initilizations and abstract methods as shown below.
int x=10; -> the signature is public static final int x=10;
void show(); -> abstract void show();
void disp();-> abstract void disp();
The properties of interface can be reuse any class with the help of implements keyword.
class classname implements InterfaceName1,InterfaceName,...
Every abstract method of interface should be overridden in the implemented class otherwise that class becomes abstract class. Note:
If any abstract method is not overridden the derived class then that derived class also becomes abstract class(every abstract method should be overridden).
The main disadvantage in abstract class is it does not support multiple inheritance,so to overcome this problem we use interface.