Interface

What is Interface in Java with Example

 

What is an Interface?

An interface is just like Java Class, but it only has static constants and abstract method. Java uses Interface to implement multiple inheritance. A Java class can implement multiple Java Interfaces. All methods in an interface are implicitly public and abstract.

Syntax for Declaring Interface

Click here if the video is not accessible 

Why is an Interface required?

To understand the concept of Java Interface better, let see an example. The class “Media Player” has two subclasses: CD player and DVD player. Each having its unique implementation method to play music.

How to use Interfaces in Java

Another class “Combo drive” is inheriting both CD and DVD (see image below). Which play method should it inherit? This may cause serious design issues. And hence, Java does not allow multiple inheritance.

How to use Interfaces in Java

Now let’s take another example of Dog.

Suppose you have a requirement where class “dog” inheriting class “animal” and “Pet” (see image below). But you cannot extend two classes in Java. So what would you do? The solution is Interface.

How to use Interfaces in Java

The rulebook for interface says,

  • An interface is 100% abstract class and has only abstract methods.
  • Class can implement any number of interfaces.

Class Dog can extend to class “Animal” and implement interface as “Pet”.

How to use Interfaces in Java

When to use Interface and Abstract Class?

  • Use an abstract class when a template needs to be defined for a group of subclasses
  • Use an interface when a role needs to be defined for other classes, regardless of the inheritance tree of these classes

Must know facts about Interface

  • A Java class can implement multiple Java Interfaces. It is necessary that the class must implement all the methods declared in the interfaces.
  • Class should override all the abstract methods declared in the interface
  • The interface allows sending a message to an object without concerning which classes it belongs.
  • Class needs to provide functionality for the methods declared in the interface.
  • All methods in an interface are implicitly public and abstract
  • An interface cannot be instantiated
  • An interface reference can point to objects of its implementing classes
  • An interface can extend from one or many interfaces. Class can extend only one class but implement any number of interfaces
  • An interface cannot implement another Interface. It has to extend another interface if needed.
  • An interface which is declared inside another interface is referred as nested interface
  • At the time of declaration, interface variable must be initialized. Otherwise, the compiler will throw an error.
  • The class cannot implement two interfaces in java that have methods with same name but different return type.

Summary:

  • The class which implements the interface needs to provide functionality for the methods declared in the interface
  • All methods in an interface are implicitly public and abstract
  • An interface cannot be instantiated
  • An interface reference can point to objects of its implementing classes
  • An interface can extend from one or many interfaces. A class can extend only one class but implement any number of interfaces
top
X
Welcome to Our Website
Welcome to WPBot
wpChatIcon