Java Interface

An Interface is a collection of abstract,static and default methods. It can have constants. Interfaces are used to abstract the interface of the classes from their implementation

As all you know java does not support multiple inheritance means a class can extend a single class only, but using interface you can achieve multi-level inheritance.

Syntax of defining an interface

interface Identifier
{
 static final dataMembers
 implicit abstract methods or static/default methods
}

Interfaces are implemented by classes.

Syntax of implementing an interface

 class className implements InterfaceName
 {
   public definition of interface abstract methods
   addition members (if any) of the class
}

NOTE: If a class implements an interface then it has to provide public definition of all abstract methods otherwise the class is declared as abstract.

Why an Interface required?

Mostly commonly asked question from an interviewer. Think while we need to define both interface and also need to provide the implementations also then why it is required.

Key Points

There are following points regarding interface.

Specification rather than implmentation

Interface makes it easy to share specifications with others without their implementation. Lots of Java API has been used interfaces so that one can easily understand their API rather to bother how it is implemented.

Likewise while you working with JDBC API you need to learn its specifications rather to think about how database vendors are going to implement it. There are number of database vendors which provide their own JDBC API implementation like MySQL,Oracle,Postgre etc.

If you are going to learn implementation then you need to learn each implementations before use. That’s why Java provides API to their programmers so that they only focus on specification part rather to think about its implementation. Like JDBC API, Java provides other APIs which consists of interfaces JAX-RS,JAX-WS,Servlet API etc and their implementation are provided by different vendors.

Mulit-Level Inheritance

As you know Java does not support multiple inheritance means a class cannot extend more than one class. But you can implement more than one interface this is called multilevel inheritance.

Marker interfaces

This kind of interface does not have any methods, but easily accessible via instanceof to discover object capabilities or wishes. Serializable and Cloneable are examples of this.

Example Of Interface

Here we have Animal as interface and it is implemented by Dog and Lion classes. There is an abstract method action() in Animal. Both Dog and Lion classes provided their own implementation.

Animal


package com.startwithjava.oops;

public interface Animal {
public void action();
}

Dog


package com.startwithjava.oops;

public class Dog implements Animal {
public void action() {
System.out.println("Dog barks");
}

}

Lion


package com.startwithjava.oops;

public class Lion implements Animal{
public void action() {
System.out.println("Lion roars");
}

}

InterfaceEx


package com.startwithjava.oops;

public class InterfaceEx {

public static void main(String[] args) {
Animal dog = new Dog();
Animal lion = new Lion();
dog.action();
lion.action();
}

}

Output

Dog barks
Lion Roars

 

Leave a Reply