By using the keyword interface we can fully abstract a class. That is, by using interface we can specify what a class must do, not how it does. By syntax of the interface are similar to the class, but they are not having any instance variables, and also the methods declared in the interface are not having any kind of body structure. In a simple meaning interfaces are generally used to create a kind of blue print.
Once an interface is created any number of classes can implement it and even particular one class can also implements any number of interfaces at any time.
To implement an interface a class must have to create complete set of methods declared in the interface. However each class is free to decide what to implement in the method of the interface.
Interface is generally used to achieve concept of multiple inheritance in java. This is because we can extends only one class at time, which does not allow to extend more than one class at a time. So the interfaces are in introduced in java, where after implements keywords in class we can write any number of interfaces in it. This is how the multiple inheritance is achieved in java.
By default methods declared in interface are public abstract and variables declared in interface are static final.
Difference between abstract and interface:
Once an interface is created any number of classes can implement it and even particular one class can also implements any number of interfaces at any time.
To implement an interface a class must have to create complete set of methods declared in the interface. However each class is free to decide what to implement in the method of the interface.
Interface is generally used to achieve concept of multiple inheritance in java. This is because we can extends only one class at time, which does not allow to extend more than one class at a time. So the interfaces are in introduced in java, where after implements keywords in class we can write any number of interfaces in it. This is how the multiple inheritance is achieved in java.
By default methods declared in interface are public abstract and variables declared in interface are static final.
Difference between abstract and interface:
No comments:
Post a Comment