Unit 9: Inheritance
Overriding Methods in Java
Introduction to Overriding Methods
In Java, when a subclass provides a specific implementation for a method that is already defined in its superclass, this is referred to as method overriding. This enables the subclass to inherit methods from the superclass and still maintain the ability to define a unique behavior if needed.
Unraveling Method Overriding
What is Method Overriding?
- Specificity in Subclasses: When a subclass needs a specific implementation for a method rather than inheriting the one from its superclass, it can override the method.
- Runtime Polymorphism: Method overriding is a perfect example of runtime polymorphism. The JVM determines at runtime which method (superclass or subclass) needs to be executed based on the object used to invoke the method.
Rules for Overriding
- Method Signature: The method in the subclass must have the same name, return type (or a subtype), and parameters as the method in the superclass.
- Access Modifiers: The access level cannot be more restrictive than the overridden method from the superclass. For instance, a public method in the superclass can't be overridden as protected in the subclass.
- Final, static, and private: Methods declared as final, static, or private cannot be overridden.
- Annotations: It's a good practice to use the @Override annotation when overriding a method. It signals the compiler to check if the method is genuinely overriding a superclass method.
Best Practice
Always use the @Override annotation when you intend to override a method. This helps the compiler catch errors in case the superclass method signature changes and the subclass method no longer overrides it.
Overriding in Action
Consider a superclass named Animal:
public class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
Now, let's override the sound method in a subclass named Dog:
public class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
}
When an object of the Dog class invokes the sound method, the overridden version in the Dog class will be executed.
The Power of Overriding
Overriding methods:
- Provides flexibility to subclasses.
- Enables polymorphic behavior.
- Allows subclasses to define their unique behavior while maintaining the structure of inheritance.
Important Note
Constructor names cannot be overridden since they must always match the class name. Hence, constructors in Java cannot be overridden.
Summary
Method overriding in Java empowers subclasses to provide specific implementations for methods inherited from their superclasses, ensuring flexibility while upholding the structure of inheritance. It's pivotal for achieving runtime polymorphism and is a crucial concept for students targeting proficiency in AP Computer Science A.
References
AP CSA Homework Assignment
Assignment: Overriding the toString() Method
Instructions
- Create a base class named Vehicle with fields brand and color.
- Override the toString() method in the Vehicle class to return a string in the format: "This is a color brand."
- Create a subclass named Car that extends Vehicle and has an additional field numberOfDoors.
- Override the toString() method in the Car class to return a string in the format: "This is a color brand with numberOfDoors doors."
- In a main method, create instances of both Vehicle and Car and print them using System.out.println.
This assignment will help you grasp the concept of method overriding, especially the significance of overriding the toString() method in custom classes.