Interface

Interface is another way to achieve abstraction.

Interface is 100% abstract class which is used to group together all similar methods with empty body.

Syntax:

Note: A class extends to another class. An interface extends another interface. But, a class also always implements single or multiple interfaces with the keyword “implements” and all its methods.

In below example Vehicle interface gets implement by the Car class.

Example:

OUTPUT:

Car

Multiple Inheritance:

Multiple Inheritance is not supported in the case of a class because of ambiguity. But, it is supported in the case of interfaces because the same method coming from different interfaces in a child class using multiple inheritance will always have a single definition.

Example:

OUTPUT:

Test User

Note:

  1. Like abstract class interface are also not allowed to create objects.
  2. All interface methods by default are public and abstract.
  3. All variables on attributes by default are public, static and final.
  4. n interface cannot contain a constructor.
  5. Interface methods do not have a body or definition.
  6. All methods need to be implemented in case of any class implements the interface.

Functional Interface

A functional interface is an interface that contains only one abstract method.

Interface will have only one functionality to perform. A functional interface can have any number of default and static methods with an implementation.

Runnable is a good example of functional interface.

Function interface plays an important role for writing lambda expression which helps us to write short and clean code.

@FunctionalInterface annotation allows us to declare a functional interface having only one abstract method.

Default Method

Java 8 introduces a new technique where it allows us to write implementation of default method in interfaces.

Single interface can have any number of default method implementation.

It provides us the backward compatibility to add some functionality for existing interfaces without changing existing logic of classes implementation the same interface.

For example, if we have an interface and same is implementing by multiple classes. After adding a default method or methods will not force us to update the existing classes implementing the same interface.

Syntax:

Static default methods:

Similar to default method interface can also have default static method implementation.

Example:

Below is the example for defining default and static methods in an interface.

OUTPUT:

Types of interfaces:

Marker Interface

It is an empty interface having no fields and methods inside it. Implementation of these interfaces will give a special definition to a class. Serializable and Clonnable interface are the example of marker interface. Cloneable interface belongs to java.lang.Cloneable and and serializable interface belongs to java.io.Serializable.

Syntax:

Public interface  {
// having no fields and methods
}

Cloneable Interface:

Cloning is similar to making a copy of an object. Below is the Employee class example where it has two fields with name and age stored in emp1 object. The clone() method will help us to make a copy of emp1 to emp2 with all field values.

Example :

OUTPUT:

Note:Accessing clone() method without implementing cloneable interface will throw CloneNotSupportedException exception

Serializable:

Serialization in java can be achieved by implementing Serializable interface.

Serialization allows us to serialize the object or write the state of object as it is into byte streams. The reverse of the serialization is deserialization where byte stream converts back to object with the same state what we serialized.

For serialization java uses writeObject() and for deserialization it is readObject().

Declaring a variable as transient inside a class will not allow to serialize or write the object into byte stream.

Imran Khan

Specialist Master (Architect) with a passion for cutting-edge technologies like AEM (Adobe Experience Manager) and a proven track record of delivering high-quality software solutions.

  • Languages: Java, Python
  • Frameworks: J2EE, Spring, Struts 2.0, Hibernate
  • Web Technologies: React, HTML, CSS
  • Analytics: Adobe Analytics
  • Tools & Technologies: IntelliJ, JIRA

🌐 LinkedIn

📝 Blogs

📧 Imran Khan