interface
Definition:
Interface is a pure abstract class that contains
public static final variables,
public Abstract methods
It provides only forcibility (specification) to subclass.
Need of interface:
Interface is invented in java for below two reasons
1) For releasing specification
2) For implementing multiple inheritance
Syntax:
[public/ { public static final data-type variable-name=variable value; ---------- -----------------------------------------------------------------; ----------------------------------------------------------------------------; public abstract return-type method-name(arguments list…); ---------- -----------------------------------------------------------------; ----------------------------------------------------------------------------; } |
Creating interface
Using keyword “interface” programmers can develop a class of type interface.
We can create public and default interface, but we can’t create private and protected interfaces
The default access specifier of interface is
Before interface-name we can place “abstract” keyword in our .java file but in compilation phase compiler removes abstract keyword and generates .class file
Variables in interfaces
In interface by default all variables are public static final variables.
Public – because it as to access from ay where.
Static – because we can’t instance interface.
Final – because it is common value for all subclases
Since interface variables are static final, it must be initialized else it leads to
CE symbol “=” expected
Methods in interfaces
In interface by default all methods are public and abstract methods
since methods has to access from anywhere outside the class.
By default interface members access specifier is public
/*this is our written code means InterfaceDemo1.java file*/
/*this is the compiler changed code means InterfaceDemo1.class file*/
/* here we can see in variable declaration missing keywords are automatically placed by compiler. and interface access specifier "public" keyword not placed, interface default access specifier is */ /* We can compile interface but we can’t execute because it doesn’t contain main method. */ |
Rules on interface
Do
Don’t
For interface either directly or indirectly can't be instantiated (creating objects). because it doesn't have a constructor. Violation leads to CompiletimeError CE:InterfaceDemo1 is abstract can't be instantiated.
But we can create reference variables of interface to store either null or its subclass object reference
In interface we can’t write concrete methods, including constructors and static and non static blocks. Violation leads to CE: interface method can’t have a body.
Inheritance with interface
We must use “implement” keyword to establish inheritance relation between class and interface
Rule on interface subclass
Subclass must implement all abstract methods of interface, else subclass should be declare as abstract otherwise it leads to CompiletimeError
These implementing methods access specifier in subclass should also be public, because interface methods are public methods.
Developing inheritance with two interface
we should use “extends” keyword to establish inheritance relation between two interfaces
Multiple inheritance using interfaces
Extending super class and implements interfaces
Extending interfaces
Difference between Interface and Abstract class