Interface in Java with Example

Let’s learn about Interface in Java. An interface in Java is similar to the class but the only difference is, Interface can have only abstract methods and final static data variables mean constant. Java interface can’t contain the implementation of methods, you can declare the abstract methods only.

Interface in Java

The interface is used to achieve the concept of Abstraction in Java.  The interface declares via the interface keyword.

Example —

 

Why We use Interface in Java

The interface is a complex topic for many beginners to understand. The first thing which confuses many peoples that you cannot define any method inside interface, it a just declaration.

By rule, all method inside interface must be abstract. So, the question is you can’t define anything, Why we need an interface?  what’s the use of an interface, if we are anyway going to write a class and override them to provide behavior, Can’t we declare those methods inside the class itself without using interface etc.

Well, if you are thinking in terms of behavior then you are really missing the point of interface. The interface is great to declare Type, they promote code reusability, and they are the real driver of polymorphism in Java.

The interface also allows multiple inheritances in Java. It allows you to write flexible code, which can adapt to handle future requirements. Some of the concrete reasons, why you need interface is –

  • If you implement methods in subclasses, the callers will not be able to call them via the interface.
  • Interfaces are a way to declare a contract for implementing classes to fulfill.
  • Interface are key of API design.

 

Video Tutorial of Automation Testing:

 

Points about Interface in Java

  • Interface functions should be public and abstract.
  • The interface is the keyword that is used to create an interface in java.
  • You can’t instantiate an interface in java.
  • You can’t declare the Interface members private & protected.
  • The class can implement the interface using keyword implements.
  • Interfaces can’t have constructors because we can’t instantiate them and interfaces can’t have a method with the body.
  • By default interface methods are implicitly abstract and public, it makes total sense because the method doesn’t have the body and so that subclasses can provide the method implementation.
  • An interface can’t extend any class but it can extend another interface.
  • A class implementing an interface must provide the implementation for all of its methods unless it’s an abstract class.
  • A class can implement more than one interface. Using this property java resolve the problem  of Multiple inheritances.

 

Example of Class implements Interface 

Output:

I am extending the first interface

 

Interface extends the interface in Java

In Java, an interface can’t extend the class but an interface can extend the another interface. If Interface A extends the interface B and Class C is implementing the interface A then Class C need to implements the method of both of the interfaces.

Output:

I am extending the first interface
Hi, I am from second Interface

 

Multiple Inheritance in Java using interface

Interface solve the problem of Multiple Inheritance like A class can implement multiple interfaces and an interface is also can extend the multiple interfaces. Please note the example where a class is implementing the multiple interfaces.

Output:

Hi, I am from first interface
Hi, I am from second Interface

0 Comment

Leave a Reply