public class Dog extends Pet
{
    private String breed;
    
    public Dog(String dName, String breed)
    {
        super(dName);
        this.breed = breed;
    }
    
    public String getBreed()
    {
        return breed;
    }
    
    public String toString()
    {
        return super.toString() + " is a dog";
    }
}

A Dog is a Pet, so Dog can extend Pet. Dog is a subclass of Pet. Pet is the superclass of Dog. An “is a” relationship between a subclass and a superclass is important. Other possible relationships, for which inheritance does not make sense, include “uses”, “has a”, or no relationship at all.

A subclass contains methods that are new (not in the superclass) or different (from the same method in the superclass). A subclass contains variables that are new (not in the superclass). There is no such thing as a variable that is different.

Dog has variable breed and method getBreed because both are new (not in Pet).

Dog has method toString because it is different than the toString method inherited from Pet. The Dog toString method overrides the Pet toString method.

Dog does not have method getName because it is neither new nor different than the getName method inherited from Pet.

Dog constructor

The purpose of a constructor, from the perspective of the class itself, is to set the initial values of the new object’s instance variables. An object of subclass type may have its own instance variables as well as instance variables inherited from its superclass. Dog declares breed and inherits name.

Instance variables inherited from a superclass retain their access specifiers. On the AP CS A Exam, all instance variables are private. A Dog object has a name instance variable; however, it can only be accessed directly by methods of Pet. The diagram below shows a Dog object. The object has separate parts for variables from Pet and from Dog, separated by a dashed line.

a box containing name at the top and breed on the bottom, separated by a dashed line

To initialize instance variables in the superclass part of an object, a subclass constructor calls a superclass constructor. The only constructor in Pet accepts a String that represents the pet’s name. In the Dog constructor, the code super(dName) calls the Pet constructor.

A subclass initializes its own instance variables the same was as any other class. In the Dog constructor, the code this.breed = breed; sets the instance variable breed to the value of the parameter breed.

Every subclass constructor must call a superclass constructor, either implicitly or explicitly. This includes a default subclass constructor, if such a constructor exists.

A subclass constructor, including a default constructor, will implicitly (automatically) call a superclass constructor if both of the following are true:

If a subclass constructor explicitly calls a superclass constructor, the call must be the first line of the subclass constructor. If a subclass constructor implicitly calls a superclass constructor, the call will occur before any other code in the subclass constructor.

Dog toString method

The Dog toString method returns "My pet ", followed by the name of the dog, followed by " is a dog". This can be accomplished in either of 2 ways.

Option 1

The code segment

return "My pet " + getName() + " is a dog";

constructs the entire desired String. getName() calls the superclass method (the one from Pet) because Dog does not have a getName method. super.getName() would also work.

Option 2

The code segment

return super.toString() + " is a dog";

calls the Pet toString method, which returns most of the desired String, then appends " is a dog". Dog has a toString method, so super. is required. Without super., the call would recursively call the Dog toString method.

Common mistake

The code segment below is incorrect since name is private in Pet. The code segment below will result in a compile time error.

return "My pet " + name + " is a dog";

Back to Inheritance and polymorphism

Comments

Comment on Inheritance & polymorphism