Interface in Java

Facebooktwitterredditpinterestlinkedinmailby feather

 

Interface in Java

In Java, the interface is just like a class that has only static constants and abstract methods. It is used to achieve polymorphism so that it can also implement multiple inheritance. All the methods in the Interface class are implicitly public and abstract.

A Java class can implement more than one interface (multiple interfaces). We can use an interface in a class by appending keyword “implements” after class name followed by the interface name.

Example

class Dog implements Animal

Java interface does not contain an implementation of methods; it only contains the signature of the method.

An interface is a 100 % abstracted class that has only abstract methods.

Since Java 8, Interfaces supported default and static methods only. Earlier versions of Java support private methods also.

Some important points about the interface are as follows.

  • The class provides body for the interface method.
  • Interface methods are abstract and public by default.
  • An interface never contains a constructor.
  • The interface declared inside the interface is known as the nested interface.

Declaration

Use of interface in java

In Java, interfaces are used for different motives like:

  • To achieve fully abstraction.
  • To achieve multiple inheritance in Java.
  • To implement abstraction.
  • To hide extra details and only show the essential information.
  • To provide security.
  • To achieve loose coupling.

To use multiple interfaces, separate every interface with a comma (,). Interfaces cannot be used to create objects.

Let’s have a look at below example.

Example1

Output

Real World example of interface

Example2

In this example, the Vehicle interface has only one method. Its implementation is provided by Car and Bike classes. In Java, an interface is defined by someone else, but its implementation is provided by different implementation providers. Moreover, it is used by someone else. In this process, the implementation part is hidden by the user who uses the interface.

Output

Multiple inheritance by interface in Java

If an interface extends multiple interfaces, or a class implements multiple interfaces, it is known as multiple inheritance.

Example

Output

Interface inheritance

Interfaces support the concept of inheritance. In Java, we can inherit an interface with another interface.

Let’s understand it with the help of TestInterface2 example.

Example

Output

 

One interface is inherited by another interface using the keyword “extends”. It is required to provide implementations for all methods required by the interface inheritance chain when a class implements an interface that inherits another interface. Let’s see an example that illustrates how one interface inherits other interfaces.

Static Method in Interface

Interfaces support static methods. They can take places in interfaces.

See at below example.

Output

Marker or Tagged interface

An interface without any member is known as a marker or tagged interface, for example, Cloneable, Serializable, Remote, etc. These interfaces are used to provide some essential information to the JVM so that JVM may perform some useful operation.

Nested interfaces

We can declare another interface within the interface, such an interface is called as a member interface or nested interface.

It can be declared as public, private, or protected. If we need to declare an interface outside the enclosing scope, it must be qualified by the name of the class or interface of which it is a member. Below is an example demonstrating the use of nested interface:

Note: On the above code, the Demo class defines a member interface called NestedIf, and that is declared public. Followed by Demo2 implements the nested interface by “implements Demo.NestedIf”.

Reason for using interface:

  • To fully abstract a class.
  • To support dynamic method resolution at run time.
  • By using interface, we can achieve multiple inheritance in java.
  • To achieve loose coupling.
Facebooktwitterredditpinterestlinkedinmailby feather