9.3 Overriding Methods
Classes can form a hierarchy where each of the subclasses has access to all of the public methods of a parent, or superclass. But what if a subclass doesn’t want to inherit a method from the superclass? For example, what if the Student
class wants to use a different toString()
method than the Person
class to include additional pieces of information about the object?
Person Class:
Student Class:
Classes like Student
are able to use their own toString
method through a process called method overriding. In a method override, a subclass can redefine a method instead of inheriting that method from the superclass.
A method override occurs when a public method in a subclass has the same method signature as a public method in the superclass. When something has the same method signature, it means that the methods have the same name and input parameters. In the example above, both toString()
methods in Person
and Student
have the same name and input parameters - the Student
toString()
, when called in the Student
class, will override the toString()
method from Person
.
To denote an override in Java, the best practice is to use the @Override
keyword in the line above the method signature. It is important to note that a method overrides a superclass when it has the same method signature, not because it includes the @Override
keyword.
Student Class:
Including @Override
is not required, but is considered best practice for two main reasons. First, using the @Override
signals to Java that you are trying to override a superclass method. The Java compiler will check to make sure it actually does override a method correctly and will throw an error if you do not override correctly. Second, it helps to make your code more readable by telling other programmers that a particular method is being used over a similar method in the superclass.
When designing class hierarchies, methods and variables that will be used by all the classes are generally put in the superclass. Subclasses are built to add additional methods and instance variables for more specific classes.
As an example, consider the Person
and Student
classes again. The Person
superclass will have name
and birthday
as the instance variables and may have a toString()
and age()
method.
The Student
class would still need the name
and birthday
, but will also need a grade
and GPA
as instance variables. These are specific only to a student, so they are added to the subclass, not the superclass. Likewise, it may include a change grade method and a modified toString()
method with an override.
When calling methods for an object, Java starts by looking in that object class for that method signature. If it finds it there, it will use that particular method, otherwise, it will go to the superclass and look for the method. This will continue until it reaches the top of the class hierarchy. Any method that is called must be defined someplace in this class hierarchy, otherwise, Java will throw an error.
-
Incorrect
Correct
No Answer was selected
Invalid Answer
Why might we want to override a superclass method? Select the best option.
Not all dogs like to bark, but some like to make a lot of noise! In this exercise, we have a Dog
superclass and a LoudDog
subclass. You do not need to modify the Dog
class.
Your task is to write two override methods in the LoudDog
class. You will override the speak()
method to return BARK!
. You will then override the toString()
so that it returns Clover is loud and likes to BARK!
where Clover is replaced by the name
variable.
Create and print at least one Dog
and one LoudDog
to test.