DEV Community

Adam Roynon
Adam Roynon

Posted on

Inheritance and Polymorphism

Inheritance is a way to base one class on another class, like a template built from an existing template. You could create a class called 'Dog' that acts as a template for all Dog objects. We could then create another class called 'Animal' that is a parent class of our 'Dog' class. All Dogs are animals, but not all animals are dogs. Our Animal class could define functionality for all Animals and then the Dog class could take all this functionality, without re-writing it, by extending/inheriting from the Animal class. The Dog class could then add more functionality, more variables, and methods, that are specific only to Dog objects.

The below code shows the declaration of an Animal class, this will be our base class. This class includes on private field variable, that is set using a parameter within the constructor. There is also one public method that returns the name field.

class Animal{

  private String name;

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

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

}
Enter fullscreen mode Exit fullscreen mode

The below code shows the declaration of a Dog class. The Dog class extends the Animal class, this is inheritance. The Dog class includes a single public method called 'bark' that returns the string value "Woof". As this class inherits from the Animal class it will also get the 'name' field as well as the 'getName' method from the Animal class, without having to rewrite any of the code within the Dog class. Within the constructor of the Dog class we have one parameter that we pass to a 'super' method. The super method refers to the base class, the Animal class. This means the name given in the Dog constructor gets passed to the underlying Animal class, that we extended from, and sets the 'name' field for our instance object.

class Dog extends Animal{

  public Dog(String name){
    super(name);
  }

  public String bark(){
    return "Woof"; 
  }

}
Enter fullscreen mode Exit fullscreen mode

This code declares and initialises a Dog object called 'buddy', and passed the string value "Buddy" to the constructor. This means the name field will be set to the value of "Buddy". We can also call the 'getName' method from the Animal class, as we have extended it. Even though we have declared the method directly in the Dog class, we can still use that functionality via inheritance. The last line calls the 'bark' method that is directly declared within the Dog class.

Dog buddy = new Dog("Buddy");
System.out.println(buddy.getName());
System.out.println(buddy.bark());
Enter fullscreen mode Exit fullscreen mode

The below code is slightly different from the above code. In this code snippet, we declare a variable called 'fido' that is declared as an Animal type, but it initialised as a Dog object. This is polymorphism. Because the Dog extends from the Animal class, we can treat it as an Animal, and declare it as an Animal variable type. We cannot do the reverse, because the Animal class does not extend from the Dog class (not all Animals are dogs).

The second line calls the 'getName' method which returns the string value "Fido", as that is the value we passed to the object's constructor. The last line calls the 'bark' method that is declared within the Dog class. This will throw an error, and not work. This is because the 'fido' variable is being treated as an Animal data type, and the Animal class does not declare the 'bark' method (the Dog class declares this method). This is why this method call won't work, as the object is declared as an Animal, not a Dog. We would have to declare the 'fido' variable as a Dog variable type to get access to this method. Remember, the left side of the equals '=' symbol is the declaration and the right side is the initialisation.

Animal fido = new Dog("Fido");
System.out.println(fido.getName());
System.out.println(fido.bark());
Enter fullscreen mode Exit fullscreen mode

This article was originally posted on my website: https://acroynon.com/

Top comments (0)