Inheritance in Java: Unlocking the Power of Object-Oriented Programming

Abu Talha
3 min readSep 22, 2023

--

inheritance is a fundamental concept in object-oriented programming (OOP) that enables the creation of new classes by inheriting properties and behaviors from existing ones. In Java, one of the most popular programming languages, inheritance plays a pivotal role in code organization, reuse, and extensibility. In this article, we’ll explore the concept of inheritance in Java, covering the basics and delving into advanced examples to illustrate its practical applications.

Understanding Inheritance

At its core, inheritance allows one class (the subclass or child class) to inherit attributes and methods from another class (the superclass or parent class). This relationship forms an “is-a” relationship, where the subclass is a specialized version of the superclass. This enables developers to create a hierarchy of classes that share common characteristics while allowing for customization as needed.

In Java, inheritance is achieved using the extends keyword. Let's start with a basic example to illustrate this concept:

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

class Dog extends Animal {
void bark() {
System.out.println(“The dog barks.”);
}
}

In this example, we have two classes: Animal and Dog. The Dog class extends the Animal class, which means it inherits the eat() method. Additionally, it defines its own method, bark(). This is a simple form of inheritance, but it illustrates the key concept.

Method Overriding

One of the powerful features of inheritance is method overriding. It allows a subclass to provide a specific implementation for a method that it inherits from its superclass. To do this, you use the @Override annotation in the child class.

class Animal {
void makeSound() {
System.out.println(“Animal makes a generic sound.”);
}
}

class Dog extends Animal {
@Override
void makeSound() {
System.out.println(“The dog barks.”);
}
}

In this example, we’ve overridden the makeSound() method in the Dog class to provide a more specific behavior. When you create an instance of Dog and call makeSound(), it will execute the overridden method, not the one from the superclass.

Superclass Constructors and super Keyword

When you extend a class in Java, the subclass inherits the constructors of the superclass. You can call these constructors using the super keyword. This allows you to reuse the initialization logic of the superclass while adding additional logic specific to the subclass.

class Animal {
String name;

Animal(String name) {
this.name = name;
}
}

class Dog extends Animal {
int age;

Dog(String name, int age) {
super(name); // Call the constructor of the superclass
this.age = age;
}
}

In this example, the Dog class has its constructor, but it also calls the constructor of the Animal class using super(name). This ensures that the name property is initialized correctly.

Multiple Inheritance

Java supports single inheritance, meaning a class can extend only one superclass. However, you can implement multiple inheritance-like behavior using interfaces. Interfaces allow a class to inherit the method signatures of multiple interfaces, effectively providing a form of multiple inheritance.

interface Swimmer {
void swim();
}

interface Flyer {
void fly();
}

class Duck implements Swimmer, Flyer {
@Override
public void swim() {
System.out.println(“The duck swims.”);
}

@Override
public void fly() {
System.out.println(“The duck flies.”);
}
}

In this example, the Duck class implements both the Swimmer and Flyer interfaces, inheriting their method signatures. This allows you to create objects of the Duck class that can both swim and fly.

Thank You!

--

--

Abu Talha
Abu Talha

Written by Abu Talha

SQA Engineer | Security Researcher | Application Penetration Tester | Back-End Developer

No responses yet