Method overriding lets a child class change how a method from its parent class works. This helps make programs flexible and clear.
Method overriding in Java
Start learning this pattern below
Jump into concepts and practice - no test required
class Parent { void show() { // parent method code } } class Child extends Parent { @Override void show() { // child method code } }
The @Override annotation is optional but helps catch mistakes.
The method in the child must have the same name, return type, and parameters as in the parent.
Dog class changes the sound method to print "Dog barks" instead of the parent's message.class Animal { void sound() { System.out.println("Animal makes a sound"); } } class Dog extends Animal { @Override void sound() { System.out.println("Dog barks"); } }
Car class overrides start to show a more specific message.class Vehicle { void start() { System.out.println("Vehicle starts"); } } class Car extends Vehicle { @Override void start() { System.out.println("Car starts with a key"); } }
This program shows method overriding. The Child class changes the greet method. When we call greet on a Child object, it uses the child's version, even if the reference is of type Parent.
class Parent { void greet() { System.out.println("Hello from Parent"); } } class Child extends Parent { @Override void greet() { System.out.println("Hello from Child"); } } public class Main { public static void main(String[] args) { Parent p = new Parent(); p.greet(); Child c = new Child(); c.greet(); Parent pc = new Child(); pc.greet(); } }
Method overriding only works with inheritance (child and parent classes).
The method signature must match exactly to override.
Using @Override helps the compiler check your code.
Method overriding lets child classes change parent class methods.
It helps customize behavior while keeping code organized.
Use @Override to avoid mistakes.
Practice
What is method overriding in Java?
Solution
Step 1: Understand method overriding concept
Method overriding means a child class changes the behavior of a method inherited from its parent class.Step 2: Compare options with definition
When a child class provides a new version of a method from its parent class correctly describes this. When a method calls itself repeatedly is recursion, C is method overloading, D is incorrect.Final Answer:
When a child class provides a new version of a method from its parent class -> Option AQuick Check:
Method overriding = child changes parent method [OK]
- Confusing overriding with overloading
- Thinking overriding changes method signature
- Mixing overriding with recursion
Which of the following is the correct syntax to override a method in Java?
class Parent {
void show() { System.out.println("Parent"); }
}
class Child extends Parent {
?
}Solution
Step 1: Check method signature and annotation
To override, method name and parameters must match exactly. Using @Override annotation helps catch mistakes.Step 2: Analyze options
private void show() { System.out.println("Child"); } uses private access modifier, narrowing from package-private (not allowed for overriding; causes hiding). void show(int x) { System.out.println("Child"); } changes parameters (overloading). void Show() { System.out.println("Child"); } has method name mismatch ('Show' vs 'show' due to case sensitivity). @Override void show() { System.out.println("Child"); } matches method signature exactly and uses @Override correctly.Final Answer:
@Override void show() { System.out.println("Child"); } -> Option BQuick Check:
@Override + same signature = correct override [OK]
- Narrowing access modifier (e.g., to private)
- Changing method parameters (overloading instead)
- Method name case mismatch
What will be the output of the following code?
class Parent {
void greet() { System.out.println("Hello from Parent"); }
}
class Child extends Parent {
@Override
void greet() { System.out.println("Hello from Child"); }
}
public class Test {
public static void main(String[] args) {
Parent obj = new Child();
obj.greet();
}
}Solution
Step 1: Understand dynamic method dispatch
When a parent reference points to a child object, overridden methods in child are called at runtime.Step 2: Trace the method call
obj is Parent type but refers to Child instance. Calling greet() runs Child's version.Final Answer:
Hello from Child -> Option DQuick Check:
Parent ref + Child object calls Child method [OK]
- Expecting parent method output
- Confusing compile-time and runtime method binding
- Thinking method overloading applies here
Identify the error in the following code snippet:
class Parent {
void display() { System.out.println("Parent"); }
}
class Child extends Parent {
@Override
void Display() { System.out.println("Child"); }
}Solution
Step 1: Check method names carefully
Parent method is 'display' (lowercase d), Child method is 'Display' (uppercase D). Java is case-sensitive, so these are different methods.Step 2: Understand @Override annotation effect
@Override expects exact match. Here, it causes a compile-time error because method names differ in case.Final Answer:
Method name case mismatch causes no overriding -> Option AQuick Check:
Method names must match exactly, including case [OK]
- Ignoring case sensitivity in method names
- Assuming @Override is optional and ignoring errors
- Confusing overriding with overloading
Consider the following classes:
class Animal {
String sound() { return "Some sound"; }
}
class Dog extends Animal {
@Override
String sound() { return "Bark"; }
}
class Cat extends Animal {
@Override
String sound() { return "Meow"; }
}
public class Test {
public static void main(String[] args) {
Animal[] animals = {new Dog(), new Cat(), new Animal()};
for (Animal a : animals) {
System.out.println(a.sound());
}
}
}What is the output of this program?
Solution
Step 1: Understand polymorphism with overridden methods
Each object calls its own overridden sound() method at runtime, even if referenced as Animal.Step 2: Trace the loop output
Dog's sound() returns "Bark", Cat's returns "Meow", Animal's returns "Some sound".Final Answer:
Bark Meow Some sound -> Option CQuick Check:
Array of Animals calls each overridden sound() correctly [OK]
- Expecting all calls to use parent method
- Thinking array cannot hold different subclass objects
- Confusing overriding with overloading
