Java OOP Concepts : Inheritance

Hello coders, I am back with another article about Java OOP concepts. Today, I am going to be explaining about Inheritance concept.

Inheritance in Java is not much different from its real meaning. Just like you have inherited the house you live in from your parents, Java classes can do the same thing.

Let me explain the concept with some code snippets so that you would be able to grasp the concept better programmatically.

Take a look at these two simple Java classes, Animal and Dog.

Animal Class
public class Animal{

    private String color;
    private String name;

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void run(){
        System.out.println("Running");
    }
    
    public void sleep(){
        System.out.println("Sleeping");
    }
}
Dog Class
public class Dog {

    public void bark(){
        System.out.println("Barking");
    }
}

As you can see Dog class does not have any attributes and just has a method. Since Dog is an Animal, we should be using the attributes from Animal class, right? How can we do that in code?

Here we make use of the extends keyword in Java. This extends keyword helps us to create a connection between Animal and Dog classes.

public class Dog extends Animal {

    public void bark(){
        System.out.println("Barking");
    }
}

This makes sure that all the properties and methods that are there in the Animal class are accessible to Dog class. Let me show you real quick.

As you can see all the methods and properties in Animal class can be used from any Dog object now.

This concept is really powerful since it helps us to write simpler and faster code and reduce code duplication.

If we didn’t have Inheritance concept, we would have had to write everything all over again in Dog class too.

Super Class and Sub Class

When we implement Inheritance in our code base, the associated classes get their special names.

The class that inherits all methods and behaviours (Dog class) is called the Subclass.

The class that gets inherited (Animal class) is called the Superclass.


Well, I don’t think I have to explain this concept that further since this is a simple concept that is already there in the real world.

Thank you for reading my article. I hope you learned something valuable from it. If you did, drop a like and follow my blog to get notifications when I publish new articles. I write articles about coding, cricket and things that I find interesting. I try to publish articles every other day.

Have a nice day ✌

Published by Thisura Thenuka

I am a passionate software engineering student. But cricket is my first love ❤

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: