An interface in Java is blueprint of a class. It can achieve abstraction and provides multiple inheritances.
Interface is similar to abstraction class but interface is purely abstract. So it is blueprint of a class instead of object instantiation. Below illustrate interface characteristics in Java.
Interface
- Interface can have only abstract methods. Since Java 8, it can have default and static methods also
- Interface supports multiple inheritance (An interface can extend multiple interfaces)
- Interface has only static and final variables.
[By default variables are declared as public, static and final]
- Interface can't provide the implementation of abstract class
- Constructor is not allowed in Interface
[There is only static fields in interface that doesn't need to be initialized during object creation in subclass and the method of interface has to provide actual implementation in subclass. So there is no need of constructor in interface]
[During the object creation of subclass, the parent constructor is called. But if there will be more than one interface implemented then a conflict will occur during call of interface constructor as to which interface's constructor will call first] - The interface keyword is used to declare interface
[By default interface is implicitly public and abstract]
No comments:
Post a Comment