Inheritance and Polymorphism

Inheritance is 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-writting 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){ = name;

  public String getName(){


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 return the string value “Woof”. As this class inherits from the Animal class it will also get the ‘name’ field aswell 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){

  public String bark(){
       return "Woof"; 


This code declares and initilises 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 us that functionality via inheritance. The last line calls the ‘bark’ method that is directly declared within the Dog class.

Dog buddy = new Dog("Buddy");

The below code is slightly different to the above code. In this code snippet we decalre a variable called ‘fido’ that is declared as an Animal type, but it initilised as a Dog object. This is polymorphism. Becuase 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 return the string value “Fido”, as that is the value we passed to the objects constructor. The last line calls the ‘bark’ method that is decalred 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 initilisation.

Animal fido = new Dog("Fido");

Leave a Reply