Javatpoint Logo
Javatpoint Logo

Can we create object of interface in Java?

In the world of Java programming, interfaces play a crucial role in defining contracts and establishing a set of rules that classes must adhere to. They serve as a blueprint for implementing classes and enable the concept of abstraction, polymorphism, and loose coupling. However, one common question that often arises among Java developers is whether it is possible to create objects of interfaces in Java. In this section, we will discuss and explore the nature of interfaces in Java.

In Java, an interface is a collection of abstract methods (methods without a body) and constants. It provides a way to define a contract that specifies what methods a class implementing the interface must implement. Essentially, an interface acts as a guide for the behavior and functionality of implementing classes.

Can we create objects of interfaces in Java?

In Java, we cannot create objects of interfaces directly because interfaces are abstract by nature. They cannot be instantiated like regular classes. Attempting to do so will result in a compilation error.

However, even though we cannot create objects of interfaces, we can achieve similar behavior using a technique called interface instantiation. The technique involves creating objects of classes that implement the interface, rather than the interface itself.

Suppose, we have an interface called Drawable, which defines a method draw() that any implementing class must implement. We cannot directly create an object of the Drawable interface, but we can create objects of classes that implement this interface.

For instance, we could have a class called Circle that implements the Drawable interface and provides its own implementation of the draw() method.

In this example, we create an object of the Circle class and assign it to a variable of type Drawable, which is an interface. Although we are not directly creating an object of the interface, we are achieving a similar effect by referencing the object through the interface type. The approach provides flexibility and allows us to switch implementations easily.

By using interface instantiation, we can leverage the power of polymorphism. We can create objects of different classes that implement the same interface, and treat them interchangeably based on the interface type. This enables loose coupling and enhances the maintainability and extensibility of the codebase.

While we cannot create objects of interfaces directly in Java, we can create objects of classes that implement the interface. The concept of interface instantiation allows us to take advantage of polymorphism and achieve flexibility and extensibility in our code. Interfaces in Java serve as contracts and guidelines for implementing classes, ensuring consistency and providing a foundation for abstraction and loose coupling.

Here's a program that demonstrates the concept of interface instantiation in Java:

InterfaceInstantiationExample.java

Output:

Drawing a circle
Drawing a rectangle






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA