Tech With Tim Logo
Go back

Inheritance

Inheritance

Inheritance is a fundamental part of object orientated programming. It allows you to inherit or use parts of other classes within a specific class. This is especially useful when you want to create two very similar classes that have very minor differences. Rather than copying the code from one class and placing it in another you can simply inherit it.

In our last tutorial we created a class called Dog.

public class Dog{
    private String name;
    private int age;

    public Dog(String name, int age){
        this.name = name;
        this.age = age;
    }

    public void speak(){
        System.out.println("I am " + this.name + "and I am " + this.age + " years old");
    }

    public int get_age(){
        return this.age;
    }

    public String get_name(){
        return this.name;
    }    

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

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

Now imagine I wanted to create a class called Cat that was identical to Dog in almost every way. Right now with our current understanding of objects we would need to copy all of the code from our dog class. This is where we use inheritance!

Parent and Child Class

There is a bit of vocabulary we need to know before moving on.

Parent Class: This is the class that is inherited from (for our example it is the Dog class).

Child Class: This is the class that inherits from a parent class (in our case the Cat class).

Note: The parent class is also known as a super class and the child class is also known as a derived class.

Extends Keyword

So lets create our Cat class and see how to inherit everything from our Dog class.

public class Cat extends Dog{
    
}

And that's all there is to it. We simply use the word extends to tell our Cat class to use everything from our Dog class.

Note: We can extend more than one class.

Protected Access Modifier

Now time to talk about the protected keyword. When dealing with inheritance we need to modify the access modifiers of our parents class attributes to be protected. This is because a private access modifier will only allow for attributes to be modified within the parent class. However, we need to be able to change those attributes from within the child class as well. This is what protected allows for, changes within any child class as well as the parent class.

So we will change the dog class to look like the following:

public class Dog{
    protected String name;  // changed to protected
    protected int age;

    public Dog(String name, int age){
        this.name = name;
        this.age = age;
    }

    public void speak(){
        System.out.println("I am " + this.name + "and I am " + this.age + " years old");
    }

    public int get_age(){
        return this.age;
    }

    public String get_name(){
        return this.name;
    }    

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

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

Super Keyword

Okay so this is all great, but what if we want to add an attribute to our Cat class called food. Well to do this we need to modify our Cat class slightly.

public class Cat extends Dog{
    private int food;

    public Cat(String name, int age, int food){
        super(name, age);
        this.food = food;
    }
}

What we've done when we use the super keyword is call the constructor of our parent class (the Dog class).

Overriding Methods

Sometimes we want to change how a method works from inside our child class. In this case we may want to change the speak method so that it says something else. To do this all we need to do is redefine the speak method in our Cat class. Java will know to use the speak method inside of the Cat class instead of the inherited one from Dog.

public class Cat extends Dog{
    private int food;

    public Cat(String name, int age, int food){
        super(name, age);
        this.food = food;
    }

    public void speak(){
        System.out.println("Meow my name is " + this.name + "and I get fed " + this.food + " food");

    }
}

Now when we call speak on a Cat object we will get a different output.

Adding Methods

Now that we've added this food attribute we should also add a getter and setter for food.

public class Cat extends Dog{
    private int food;

    public Cat(String name, int age, int food){
        super(name, age);
        this.food = food;
    }

    public int get_food(){
        return this.food;
    }

    public void set_food(int food){
        this.food = food;
    }
}

Now our Cat class will have all of the getters and setters from our dog class as well as ones for its own food attribute.

Creating a Cat Object

Hopefully an example can help illustrate what I've discussed above.

public class Main{
    public static void main(String[] args){
        Cat c1 = new Cat("tim", 5, 10); // Creating a cat object
        Dog d1 = new Dog("joe", 7); // Creating a dog object

        c1.speak() // This will output "Meow my name is tim and I get fed 10 food" 
        d1.speak() // This will output "My name is joe and I am 7 years old"

        c1.set_age(20) // We can use this method even though it is only defined in the dog class
        
        d1.get_food() // See if you can figure out why this line will cause an error
    }   

}
Design & Development by Ibezio Logo