Interface and Inheritance in Java: Interface

Share this article

  Interface is a 100% abstract class. It contains only constants and method signatures. In other words it is a reference type similar to class. An interface can’t be instantiated. It can be implemented by a class or extended by another interface.

How To Define:

An interface can be defined as the following:
public interface DriveCar { 
void turnLeft(); 
void turnRight();
void moveBack(); 
void accelerate();
}
The methods declared in an interface don’t have method bodies. By default all the methods in an interface are public abstract. Similarly all the variables we define in an interface are essentially constants because they are implicitly public static final. So, the following definition of interface is equivalent to the above definition.
public interface DriveCar { 
public abstract void turnLeft(); 
public abstract void turnRight(); 
public abstract void moveBack(); 
public abstract void accelerate(); 
}

How To Use:

An interface defines a contract which an implementing class must adhere to. The above interface DriveCar defines a set of operations that must be supported by a Car. So, a class that actually implements the interface should implement all the methods declared in the interface. Example:
class Car implements DriveCar{ 
void turnRight(){ 
//implementation code goes here 
} 

void turnLeft(){ 
//implementation code goes here 
}

void moveBack(){ 
//implementation code goes here 
}

void accelerate(){ 
//implementation code goes here 
}

}
Now we can take a reference of type DriveCar
and assign an object of Car to it. Example:
DriveCar carDriver=new Car(); 
carDriver.turnLeft(); 
carDriver.moveBack(); 
//other method invocations 
We can also code in the following way: Example:
Car car=new Car(); 
car.turnLeft(); 
car.moveBack(); 
//other method invocations 

Why Use Interfaces:

Interfaces act as APIs (Application Programming Interfaces). Let us take an example of an image processing company which writes various classes to provide the image processing functionalities. So, a nice approach will be creating interfaces, declaring the methods in them and making the classes implement them. In this way the software package can be delivered to the clients and the clients can invoke the methods by looking at the method signatures declared inside the interfaces. They won’t see the actual implementation of the methods. As a result the implementation part will be a secret. Later on the company may decide to re-implement the methods in another way. But the clients are concerned about the interfaces only. Interfaces provide an alternative to multiple inheritance. Java programming language does not support multiple inheritance. But interfaces provide a good solution. Any class can implement a particular interface and importantly the interfaces are not a part of class hierarchy. So, the general rule is extend one but implement many. A class can extend just one class but it can implement many interfaces. So, here we have multiple types for a class. It can be of the type of its super class and all the interfaces it implements. Example: Let us say we have two interfaces A & B and two classes C & D.
interface A{ } 
interface B{ } 
class C{ } 
class D extends C implements A,B { } 
So, we can have 3 types for an object of class D as following:
A a=new D(); B b=new D(); C c=new D(); 
Class Diagram But be careful. If you use interface as reference type and assign an object of implementing class to it then you can call only those methods that are declared inside the interface. This is quite obvious because the implementing class can define methods of its own that are not a part of the contract between the interface and class. So, to call those methods you have to use the class as reference type as following: D d=new D();

Extending an interface:

Consider the following scenario. You have an interface A and several implementing classes. It defines 2 methods.
interface A{ 
int doThis(); 
int doThat(); 
} 
Now suppose you want to add another method to the interface A:
interface A{ 
int doThis(); 
int doThat(); 
int doThisAndThat(); 
} 
If you add the third method to the interface it will break the code because the implementing classes will no more be adhering to the contract. But we can avoid the problem if we create another interface and make it extend the previous interface.
interface APlusPlus extends A{
int doThisAndThat(); 
} 
Now your users have the option to either use the old interface or upgrade to the new interface. Note: Any class that implements an interface must implement the methods declared in that interface plus all the methods that are present in the super interface. If the implementing class is abstract it may choose to implement all, some or none of the methods declared in the interface. But a concrete subclass of the abstract class must implement all the non implemented methods.

Summary:

  • Interfaces can contain only constants and method signatures, but no implementation.
  • Interfaces cannot be instantiated. They can only be implemented by an implementing class or extended by another interface.
  • A class that implements an interface must provide implementation to all the methods that are declared in the interface.
  • Interfaces can be used as reference type for the object of an implementing class.
  • An interface can be extended by another interface.

Frequently Asked Questions (FAQs) about Interface and Inheritance in Java

What is the main difference between an interface and a class in Java?

In Java, a class is a blueprint from which individual objects are created. It can contain fields and methods to describe the behavior of an object. On the other hand, an interface is a completely abstract class that can only contain abstract methods and constants. It is used to achieve full abstraction and multiple inheritance in Java. Unlike classes, interfaces cannot be instantiated and can only be implemented by classes or extended by other interfaces.

Can an interface inherit from another interface in Java?

Yes, in Java, an interface can inherit another interface using the ‘extends’ keyword. This is known as interface inheritance. The child interface inherits all the methods of the parent interface. However, it’s important to note that the methods remain abstract in the child interface.

How can we achieve multiple inheritance in Java?

Java does not support multiple inheritance with classes due to the “Diamond Problem”. However, multiple inheritance can be achieved in Java through interfaces. A class can implement multiple interfaces, and an interface can extend multiple interfaces. This allows a class to inherit the properties of multiple interfaces.

Can a class implement multiple interfaces in Java?

Yes, a class in Java can implement multiple interfaces. When a class implements multiple interfaces, it needs to provide the implementation for all the methods declared in the interfaces.

What is the purpose of default methods in an interface?

Default methods were introduced in Java 8 to allow developers to add new methods to an interface without breaking the classes that implement this interface. Default methods have a default implementation, which can be overridden by the classes that implement the interface.

Can we declare variables in an interface?

Yes, we can declare variables in an interface. However, these variables are implicitly public, static, and final. That means their values are constant and cannot be changed once assigned.

What is a functional interface in Java?

A functional interface in Java is an interface that contains only one abstract method. They can have any number of default or static methods. Functional interfaces are used as the basis for lambda expressions in functional programming.

Can we create an object of an interface?

No, we cannot create an object of an interface in Java. However, we can create a reference variable of an interface which can point to the object of the class that implements that interface.

What is the difference between extends and implements keywords in Java?

The ‘extends’ keyword is used in Java for the inheritance of classes and interfaces. On the other hand, the ‘implements’ keyword is used by classes to implement an interface. A class can extend only one class but can implement multiple interfaces.

Can an interface extend a class in Java?

No, an interface cannot extend a class in Java. An interface can only extend another interface. However, a class can implement an interface and extend another class.

Sandeep PandaSandeep Panda
View Author

Sandeep is the Co-Founder of Hashnode. He loves startups and web technologies.

Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week