Reading:
Inheritance in Java - What Is Inheritance?

Inheritance in Java - What Is Inheritance?

Metamug
Inheritance in Java - What Is Inheritance?

In Java, inheritance allows us to reuse code by creating subclasses that inherit all the properties and methods of a parent class.

What is inheritance?

In inheritance, we inherit the properties and methods of a parent class. This means that any changes made to the parent class will automatically be inherited by its child classes.

How inheritance works?

Inheritance is one of the fundamental concepts of object oriented programming (OOP). It allows us to reuse code and reduce the number of lines of codes required to write programs. Inheritance works by creating a child object based on the parent object. This means that any properties and methods defined in the parent will also be available to the child. It's not necessary to define every property and method in the child class because the parent class has them.

//base class
public interface Animal{

}
//sub class
public class Dog extends Animal{

}

//sub class
public class Cat extends Animal{

}

When should we use inheritance ?

There are several reasons why we use inheritance. One reason is to reuse code. If we have a lot of similar code, we can put it into a base class and then extend that class with different features. Another reason is to make our programs more flexible. If we need to add new functionality later, we can just add it to the base class instead of having to rewrite everything.

Object creation with superclass reference

Creating objects with superclass reference, allows a common type for the objects.

    Animal tom = new Cat();
    Animal milo = new Dog();

    List<Animal> animals = new ArrayList<>();
    animals.add(tom); //added cat object
    animals.add(milo) //added dog object

    for(Animal a:animals){

    }

It is easier to iterate over the objects as they are of the same type. Using the instanceof keyword we can check which subclass the object belongs to in the for loop.

Example Code

Here is an example of inheritance in Java. We start with a base class called Animal. Then we create two subclasses of Animal called Dog and Cat. In both cases, we inherit from Animal.

//base class
interface Animal{
    public String speak();
}
//sub class
class Dog extends Animal{
    @Override
    public String speak(){
        return bark();
    }
}

//sub class
class Cat extends Animal{
    @Override
    public String speak(){
        return meow();
    }
}

public class Main{
    public static void main(String[] args) {
        Animal tom = new Cat();
        Animal milo = new Dog();

        List<Animal> animals = new ArrayList<>();
        animals.add(tom); //added cat object
        animals.add(milo) //added dog object

        for(Animal a:animals){
            System.out.println(a.speak());
        }
    }
}

The above speak() method shows polymorphic behavior depending on subclass object



Comments

Post a comment
Icon For Arrow-up