Javatpoint Logo
Javatpoint Logo

Extends Vs. Implements in Java

Java is a versatile and widely used programming language that provides a number of features for developing complex and efficient software applications. The two main concepts in Java object-oriented programming are extensions and implementations. These two keywords play an important role in class relations and how classes and networks interact. In this section, we will explore the basic differences between extends and implements in Java and when to use each.

Extensive: Inheritance Between Classes

The extends keyword is used to establish property relationships between classes. When a class extends another class, it inherits all the attributes and behaviors (fields and methods) of the superclass (the extended class). A subclass is an essential part of a superclass in this respect. It is often referred to as an "is-a" relationship, where the subclass is a kind of "is-a" superclass.

Here's a basic example:

In the code above, the Car class extends the Vehicle class. As a result, the Car class inherits the start() method from Vehicle and can also define its own method, accelerate(). This is an obvious example of sequencing, with a significant portion of the Vehicle.

Applications: Interface functionality

The "implements" keyword is used when a class wants to follow an interface. In Java, an interface defines a contract, specifying which method signatures each class implementing the interface must provide concrete functionality. Courses can use multiple interfaces, allowing them to display different behaviors.

Here's an example:

The Bird class in this code implements the Flyable interface. By doing so, it promises to provide an implementation for the fly() method. It can also define its own methods, such as chirp(). This refers to the concept of an interface implementation, which is used to access a number of properties in Java.

Key differences: Extends vs Implements

  • Inheritance vs interfaces: The main difference is that "extensions" establish direct inheritance relationships between classes, while "tools" are used to manipulate interfaces A class can only extend one superclass, but can manipulate multiple interfaces role.
  • Method inheritance When a class extends another class, it accepts all the fields and methods of the superclass. In contrast, using an interface only requires the use of methods declared in that interface.
  • "is-a" vs. "can do": "Extensive" refers to an "is-a" relationship, where a subclass is a specialized version of the superclass. "Implements" refers to a "can do" relationship, where the class can perform any actions specified by the interface.
  • Extending classes, using interfaces: One or more interfaces can be used at the same time to extend a class. This allows the class to inherit the claim and comply with the contract.

When to use Extends and Implements?

Use "extends" when we want to create a new class that takes the behavior of an existing class and is unique. It often happens when instantiating hierarchical classes, where each subclass fixes and extends the functionality of the superclass.

If we want to define a contract for a class, use "implements" to ensure that it provides specific methods. It is valuable when we want to have polymorphism and enable different objects to share common actions through interfaces.

Here's a complete Java code example that demonstrates the use of both extends and implements to differentiate class inheritance and interface implementation:


Circle Area: 78.53981633974483
Rectangle Area: 24.0
Drawing a circle.
Drawing a rectangle.

In this code, the Circle and Rectangle classes both extend the Shape class, inheriting the area method through class inheritance (using "extends"). Additionally, both classes implement the Drawable interface, providing an implementation for the draw method, demonstrating interface implementation (using "implements").

The main() method creates instances of the Circle and Rectangle classes, calculates their respective areas, and invokes the draw method to showcase the difference between class inheritance and interface implementation in action.

Here's a breakdown of the output:

The Circle object calculates and prints its area, which is approximately 78.54 (π * 5 * 5).

The Rectangle object calculates and prints its area, which is 24 (4 * 6).

The draw method for the Circle class is invoked, which prints "Drawing a circle."

The draw method for the Rectangle class is invoked, which prints "Drawing a rectangle."

The output demonstrates the difference between class inheritance ("extends") and interface implementation ("implements") in action. The Circle and Rectangle classes both inherit the area method from the Shape class (class inheritance), and they implement the Drawable interface to provide specific drawing behavior (interface implementation).

To summarize, "extends" are used for class properties, while "implements" are used for interface implementations. The choice between the two depends on your design requirements and the relationships we want to establish in the Java code. Understanding these concepts is critical to building well-structured and maintainable Java applications.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA