DEV Community

Mercy
Mercy

Posted on

Inheritance with classes

When we discuss inheritance in the context of an object-oriented programming language such as Java, we talk about how a class can inherit the properties and behavior of another class. The class that inherits from another class can also define additional properties and behaviors.

What is inheritance

Inheritance is a fundamental concept in object-oriented programming (OOP) that facilitates the creation of hierarchical relationships between classes. In Java, inheritance allows a new class, known as a subclass or child class, to inherit attributes and behaviors (methods) from an existing class, referred to as a superclass or parent class. This mechanism promotes code reusability and establishes a natural organization of classes within a system.

The syntax for implementing inheritance in Java is straightforward. A subclass is defined using the extends keyword, followed by the name of the superclass. For example, if we have a class named Animal, a subclass Dog can be created as follows:

class Animal {  
    void eat() {  
        System.out.println("This animal eats food.");  
    }  
}  

class Dog extends Animal {  
    void bark() {  
        System.out.println("The dog barks.");  
    }  
}
Enter fullscreen mode Exit fullscreen mode

In this example, the Dog class inherits the eat() method from the Animal class, enabling it to exhibit the behavior defined in the parent class while also introducing its own unique behavior, represented by the bark() method.

One of the key advantages of inheritance is code reusability. Instead of redefining common functionality across multiple classes, a developer can implement shared methods in a superclass, thereby reducing redundancy and fostering maintainability. Moreover, inheritance allows for polymorphism, which enables the use of a superclass reference to refer to a subclass object. This versatility is crucial in designing flexible and scalable applications.

Let’s get started with the need to inherit classes.\

The need to inherit classes

Imagine the positions Programmer and Manager within an organization. Both of these positions have a common set of properties, including name, address, and phone number. These positions also have different properties. A Programmer may be concerned with a project’s programming languages, whereas a Manager may be concerned with project status reports.

Let’s assume you’re supposed to store details of all Programmers and Managers in your office.Below, I will show the properties and behavior that you may have identified for a Programmer and a Manager, together with their representations as classes.

Properties and behavior of a Programmer and a Manager, together with their representations as classes

Image description
Did you notice that the classes Programmer and Manager have common properties, namely, name, address, phoneNumber, and experience? The next step is to pull out these common properties into a new position and name it something like Employee.

Identify common properties and behaviors of a Programmer and a Manager, pull them out into a new position, and name them Employee.

Image description
This new position, Employee, can be defined as a new class, Employee, which is inherited by the classes Programmer and Manager. A class uses the keyword extends to inherit a class.

The classes Programmer and Manager extend the class Employee.

Image description

Inheriting a class is also referred to as subclassing. In the above inherited class Employee is also referred to as the superclass, base class, or parent class. The classes Programmer and Manager that inherit the class Employee are called subclasses, derived classes, extended classes, or child classes.

Conclusion
To conclude, inheritance is a powerful feature in Java that enhances code reusability and promotes a hierarchical class structure. When used thoughtfully, it can significantly improve the efficiency and clarity of software development in object-oriented environments.

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

Top comments (0)

Image of Docusign

🛠️ Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

👋 Kindness is contagious

Discover a treasure trove of wisdom within this insightful piece, highly respected in the nurturing DEV Community enviroment. Developers, whether novice or expert, are encouraged to participate and add to our shared knowledge basin.

A simple "thank you" can illuminate someone's day. Express your appreciation in the comments section!

On DEV, sharing ideas smoothens our journey and strengthens our community ties. Learn something useful? Offering a quick thanks to the author is deeply appreciated.

Okay