Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 2 of 9
Method Overriding & @Override
Overriding lets a subclass provide its own implementation of a method inherited from the superclass. The @Override annotation tells the compiler your intent - it catches typos and signature mismatches at compile time. Runtime polymorphism depends on correct overriding.
Overriding vs Overloading
- Overriding = same method name + same parameter types in subclass. RUNTIME decision.
- Overloading = same method name + different parameter types in same class. COMPILE-TIME decision.
- @Override annotation: not required but ALWAYS use it - compiler verifies you're actually overriding.
- Covariant return types: override can return a SUBTYPE of the original return type.
- Cannot override: static methods (they can be hidden, not overridden), private methods, final methods.
Overriding in Practice
public class LibraryItem {
public String getDisplayInfo() {
return String.format("[%s] %s", getId(), getTitle());
}
public double calculateLateFee(int daysLate) {
return daysLate * 0.50; // default: $0.50/day
}
}
public class Book extends LibraryItem {
private double price;
// Override: add price to display
@Override // ← always use this. Without it, a typo becomes a new method silently
public String getDisplayInfo() {
return super.getDisplayInfo() + String.format(" | $%.2f", price);
}
// Override: books have higher late fee than default
@Override
public double calculateLateFee(int daysLate) {
return daysLate * 1.00; // $1.00/day
}
}
public class RareBook extends Book {
@Override
public double calculateLateFee(int daysLate) {
// Rare books: base fee + 50% surcharge
return super.calculateLateFee(daysLate) * 1.50;
}
}
// Usage demonstrating runtime dispatch
LibraryItem item1 = new Book(...);
LibraryItem item2 = new RareBook(...);
// Calls Book's calculateLateFee at runtime (not LibraryItem's)
System.out.println(item1.calculateLateFee(5)); // 5.00
// Calls RareBook's calculateLateFee at runtime
System.out.println(item2.calculateLateFee(5)); // 7.50Quick Quiz
Tip
Tip
Practice Method Overriding Override in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Visual architecture and concepts for Method Overriding & @Override
Practice Task
Note
Practice Task - (1) Write a working example of Method Overriding Override from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Common Mistake
Warning
A common mistake with Method Overriding Override is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready java code.
Key Takeaways
- Overriding lets a subclass provide its own implementation of a method inherited from the superclass.
- Overriding = same method name + same parameter types in subclass. RUNTIME decision.
- Overloading = same method name + different parameter types in same class. COMPILE-TIME decision.
- @Override annotation: not required but ALWAYS use it - compiler verifies you're actually overriding.
Finished this lesson?