INH - Overriding Methods (Lesson)

APCompSci_LessonTopBanner.png

Overriding Methods

Introduction

Methods from a superclass can be overridden in a subclass. An overridden method is one that has the same return type, signature (name) and parameters but different statements. Think of overriding a file on your computer. When you save a file with the same name as another file you are overriding it.

Method Overriding

Let’s consider a sport inheritance hierarchy that has a Sport superclass (parent class) and Tennis and Basketball subclasses (child classes).

MethodOverriding_Image.png

In this example, Sport is the parent class. Tennis and Basketball are the child classes. The child classes will inherit all public and protected methods. The Tennis class will have the scorePoint(), startGame() and endGame() methods. The Basketball class will have the scorePoint(), startGame(), endgame() and the wearJersey() methods. Notice that both the Tennis class and the Basketball class have their own scorePoint() methods. These are examples of overridden methods. Method overriding occurs when a public method in a subclass has the same method signature as a public method in the superclass. It makes sense that Tennis and Basketball will have their own scorePoint methods since scoring points would happen differently for each sport.

Any method that is called must be defined within its own class or its superclass. When code is written and methods are called, the compiler will first look in the current class for the method. If it is not found it will then go up the inheritance hierarchy to check the parent or even the grandparent class until the method is found.

A subclass is usually designed to have modified (overridden) or additional methods or instance variables. A subclass will inherit all public methods from the superclass; these methods remain public in the subclass.

super keyword in Methods

Consider the following Parent and Child class.

public class Parent
{
public void methodA()
{
System.out.print1n("Wonk");
}
public void methodB ()
{
System.out.print1n("Honk");
}
}
public class Child extends Parent
{
public void methodA()
{
System.out.print1n("Stomp");
}

public void methodB ()
{
super.methodA ();
System.out.print1n("Splat:);
}
} 

The Parent class has two methods, methodA() and methodB(). The Child class also has those two methods, but they have been overridden so they will do different actions than the methods in the parent class.

Notice, methodB() in the Child class calls methodA() from the parent class using the keyword super. Then it continues with the next statement in method.

 

Book Icon Click on "Runestone Academy" below to open the required reading that is listed.

Runestone Academy: AP CSA – Java Review Links to an external site.

  • READ: 11.8 – Overriding vs Overloading

Practice Icon Click on "Introduction to Computer Science Using Java" below to open the practice quiz.

Introduction to Computer Science Using Java Links to an external site.

  • Chapter 82:
  • Complete the quiz for practice.

Practice-It! Practice

Practice Icon Go to the PracticeIt website Links to an external site..

Log into account.

Click on Start Practicing!

Go to the most recent edition.

Click on Chapter 9: Inheritance and Interfaces

Complete Exercises 9.4 & 9.9

APCompSci_LessonBottomBanner.pngIMAGES CREATED BY GAVS