04 Interface
04 Interface
INTERFACE.
Interface
Define Interface, implementing interface,
Accessing interface, variables & methods,
Extending interfaces,
Interface references,
Nested interfaces
INTERFACE
As we know about classes and how they can inherited by other classes.
Multiple Inheritances is Not Supported by Java through Classes.
Use of multiple inheritance (or Hybrid Inheritance) proves difficult and adds complexity to the language so
Java does not support multiple inheritance.
That is, classes in Java can not have more than one super class.
If you try to give the definition like
o class A extends B, extends C {….} or
o class A extends B,C
The extention of multiple classes is not permitted in Java.
New Approach of Multiple Inheritances in Java.
Java provides an alternate approach known as interfaces to support the concept of multiple inheritances.
A large number of real life applications require the use of multiple inheritance, through which we inherit
methods and properties from several distinct classes.
Though multiple inheritance is not supported by java directly but it is implemented by the use of interface.
Need Of Interface
Java allow have more than one super class in the form of interface.
To achieve multiple Inheritance.
We can implement one or more Interface(s) in the one class.
Methods can be implemented by one or more class(s).
Features of Interface
The Variable of an interface :
o are explicitly declared final and static (as constant).
o The class which implements interface, cannot change them.
o They must be initialize with a constant value.
o All the variable are implicitly public of the interface.
The Method declaration of :
o Interface contains only a list of methods without any statement.
o Methods declaration terminated with a semicolon.
o The method are by default abstract.
o So the classes which implements the interface, must override all the methods of interface with public
access.
HOW TO ACHIEVE MULTIPLE INHERITANCE WITH INTERFACE.
It is a type of inheritance where a derived class may have more than one parent class.
It is not possible in case of java as you cannot have two classes at the parent level Instead there can be one
class and many interfaces at parent level to achieve multiple interface.
Interface is similar to classes but can contain on final variables and abstract method.
Interfaces can be implemented to a derived class.
Syntax
Example
DEFINING AN INTERFACE:
Defining an interface is just like defining a class, except that the class keyword is replaced with the interface
keyword, and abstract methods and constant fields (data members) are allowed in an interface.
Every interface is by default abstract, all it’s methods are by default declared as abstract and public. So you do
not need to explicitly put the abstract or public modifiers before them.
Similarly, all the data members declared in an interface are by default public static and final. They must be
initialize with the constant value. We do not need to explicitly insert the final, static or public modifiers before
them.
FEATURES OF INTERFACE:
Related To Data Members.
o Variable of an interface are explicitly declared final and static (as constant) meaning that the
implementing the class cannot change them.
o They must be initialize with a constant value.
o All the variable are implicitly public of the interface, itself, is declared as a public
Related To Methods.
o Method declaration contains only a list of methods without any body statement and ends with a
semicolon.
o The method are, essentially, abstract methods.
o There can be default implementation of any method specified within an interface.
o Each class that include an interface must implement all of the method
NEED OF INTERFACE:
o To achieve multiple Inheritance.
o We can implement more than one Interface in the one class.
o Methods can be implemented by one or more class.
6 The memory is allocated for the classes. We are not allocating the memory for the interfaces.
EXTENDING AN INTERFACE.
Interfaces can form hierarchies just like classes.
Interface can never extends a normal class, but it extends another interface.
Interface uses extends clause (keywrod) to create a new sub_interface, from the existing super_interface.
The sub_interface data members are public static final and methods are abstract.
Important : it is necessary for the class that implements the sub_interface, to override all sub_interface
methods and it’s super_interface method.
Syntax : interface sub_interface extends super_interface { ……….. }
Example:
interface Qualification
{
String degree = “Engeneering”; //minimum Qualification
int percentage = 60; // minimum percentage
void showRequirement(); // To print the Requirement
}
interface Trainee extends Qualification
{
int stipend = 3000; // minimum monthly salary
int lectures = 18; // no weekly lecture allotted
void showInfo(); // To print the Working information of Trainee
}
NESTED INTERFACE
An interface which is declared within another interface or class is known as nested interface.
The nested interfaces are used to group related interfaces so that they can be easy to maintain.
The nested interface must be referred by the outer interface or class.
It can't be accessed directly.
Some points that should be remembered in nested interface.
o Nested interface must be public if it is declared inside the interface but it can have any access modifier if
declared within the class.
o Nested interfaces are declared static implicitely.
We can define nested interface in interface or class.
Syntax Interface within Interface:
Access interface InterfaceName
{
//Members of Outer Interface
public interface nested_InterfaceName
{
//Memebers of Nested Interface
}
}
Syntax Interface within class:
Access class ClassName
{
//Members of Class
Access interface nested_InterfaceName
{
//Memebers of Nested Interface
}
}