DEV Community

Cover image for Understanding Inheritance in Java Through a Practical Example
Edwin Kinyao
Edwin Kinyao

Posted on

Understanding Inheritance in Java Through a Practical Example

Understanding Inheritance in Java Through a Practical Example

Inheritance is a core concept in object-oriented programming (OOP) that allows one class to acquire the properties (attributes and methods) of another class. In Java, inheritance is implemented using the extends keyword and represents an "is-a" relationship. This article explains inheritance in Java through a practical example.

The Code Example

// Defining a class
class Animal {
    // General attributes
    protected String colour;
    protected String breed;
    protected int age;

    // General methods
    public String sleep() {
        return "Both cats and dogs sleep";
    }

    public String eat() {
        return "They also eat";
    }

    // Constructor
    public Animal(String colour, String breed, int age) {
        this.colour = colour;
        this.breed = breed;
        this.age = age;
    }

    // Getters and setters
    public String getColour() {
        return colour;
    }

    public void setColour(String colour) {
        this.colour = colour;
    }

    public String getBreed() {
        return breed;
    }

    public void setBreed(String breed) {
        this.breed = breed;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

// Cat class inheriting from Animal class
class Cat extends Animal {
    private String catName;

    public Cat(String colour, String breed, int age, String catName) {
        super(colour, breed, age); // Call the parent class constructor
        this.catName = catName;
    }

    public String getCatName() {
        return catName;
    }

    public void setCatName(String catName) {
        this.catName = catName;
    }

    public String catSound() {
        return "Cat meows!";
    }
}

// Dog class inheriting from Animal class
class Dog extends Animal {
    private String dogName;

    public Dog(String colour, String breed, int age) {
        super(colour, breed, age);
    }

    public String getDogName() {
        return dogName;
    }

    public void setDogName(String dogName) {
        this.dogName = dogName;
    }

    public String dogSound() {
        return "Dog barks!";
    }
}

public class Demo {
    public static void main(String[] args) {
        Cat myCat = new Cat("Brown", "Persian", 2, "Tom");
        Dog myDog = new Dog("Black", "Labrador", 3);

        // Display Cat details
        System.out.println("Cat's Name: " + myCat.getCatName());
        System.out.println("Cat's Colour: " + myCat.getColour());
        System.out.println("Cat's Breed: " + myCat.getBreed());
        System.out.println("Cat's Age: " + myCat.getAge());
        System.out.println("Cat Sound: " + myCat.catSound());
        System.out.println("Cat Behavior: " + myCat.eat() + " and " + myCat.sleep());

        // Display Dog details
        System.out.println("Dog's Colour: " + myDog.getColour());
        System.out.println("Dog's Breed: " + myDog.getBreed());
        System.out.println("Dog's Age: " + myDog.getAge());
        System.out.println("Dog Sound: " + myDog.dogSound());
    }
}
Enter fullscreen mode Exit fullscreen mode

Key Concepts in the Code

Parent Class (Animal):

  • Defines common attributes (colour, breed, age) and methods (sleep, eat) that are shared among all animals.
  • Provides a constructor to initialize these attributes.
  • Includes getters and setters for encapsulation.

Child Classes (Cat and Dog):

Image description
Image description

  • Extend the Animal class and inherit its attributes and methods.
  • Add specific attributes (catName, dogName) and behaviors (catSound, dogSound).
  • Use the super keyword to call the parent class constructor and initialize inherited attributes.

Demo Class:

  • Serves as the entry point of the program.
  • Demonstrates how to create objects of Cat and Dog classes and access their properties and methods.

Benefits of Inheritance

  • Code Reusability: The Cat and Dog classes reuse the code in the Animal class.
  • Extensibility: New child classes (e.g., Bird, Fish) can be added easily by extending the Animal class.
  • Polymorphism: Shared methods like sleep and eat can be overridden in child classes to provide specific behaviors.

Output of the Program

Cat's Name: Tom
Cat's Colour: Brown
Cat's Breed: Persian
Cat's Age: 2
Cat Sound: Cat meows!
Cat Behavior: They also eat and Both cats and dogs sleep
Dog's Colour: Black
Dog's Breed: Labrador
Dog's Age: 3
Dog Sound: Dog barks!

Enter fullscreen mode Exit fullscreen mode

my GitHub
java repo

Top comments (0)