Interfaces

Interface is a class that contains only abstract methods and no concrete methods. This means an interface contains methods which are all abstract and hence none of the methods will have body. Only method prototypes will be written in the interface. So, an interface can be defined as a specification of method prototypes.

Interface Class

An interface contains only abstract methods which are all incomplete methods. So, it is not possible to create an object to an interface. In this case, we can create separate classes where we can implement all the methods of the interface. These classes are called implementation classes. Since, implementation classes will have all the methods with body, it is possible to create objects to the implementation classes.

Since, none of the methods have body in the interface, we may tend to think that writing an interface is waste, but this is not correct. In fact, an interface is more useful when compared to the class owing to its flexibility of providing necessary implementation for the objects.

Syntax Of Interface

interface interface_name
{
  Variable;
  Method;
}

Points To Know About Interface

  • An interface cannot implement another interface.
  • It is possible to write a class within an interface.
  • An interface can extend another interface.
  • An interface is a specification of method prototypes.
  • An interface will have 0 to more abstract methods which are all public and abstract by default
  • None of the methods in an interface can be private, protected or static
  • We cannot create an object to an interface, but we can create a reference of interface type
  • When an interface is written, any third party vendor can provide implementation classes to it
  • Interface forces the implementation classes to implement all of its methods. Java compiler checks whether all the methods are implemented in the implementation classes or not.
  • Interface reference can refer to the objects of its implementation classes.
  • An interface can have variables which are public static and final by default. This means all the variables of the interface are constants.

Multiple Inheritance Using Interfaces

We know that multiple inheritance, sub classes are derived from multiple super classes. If two super classes have same names for their members (variables and methods) then which member is inherited into the subclass is the main confusion in multiple inheritance. This is the reason, Java does not support the concept of multiple inheritance. This confusion is reduced by using multiple interfaces to achieve multiple inheritance.

// Multiple inheritance using interfaces
interface Father{
    float height = 6.2f;
    void getHeight();
}
interface Mother{
    float height = 5.8f;
    void getHeight();
}
class Child implements Father, Mother{
    public void getHeight(){
        //child got average height of its parents
        float height = (Father.height + Mother.height)/2;
        system.out.println("Child's height ="+ height);
    }
}
class Multi {
    public static void main(String args[]){
        Child ch = new Child();
        ch.getHeight();
    }
}

The output is

Child's height = 6.0

Subscribe For More Content