Method overriding lets a child class change how a method from its parent works. This helps make programs flexible and easy to update.
Method overriding rules in Java
Start learning this pattern below
Jump into concepts and practice - no test required
class Parent { void show() { System.out.println("Parent show method"); } } class Child extends Parent { @Override void show() { System.out.println("Child show method"); } }
The method in the child class must have the same name, return type, and parameters as the parent method.
Use the @Override annotation to help the compiler check you are correctly overriding.
Dog changes the sound method to print a dog-specific message.class Animal { void sound() { System.out.println("Animal sound"); } } class Dog extends Animal { @Override void sound() { System.out.println("Dog barks"); } }
Car class overrides speed to return a different number.class Vehicle { int speed() { return 50; } } class Car extends Vehicle { @Override int speed() { return 100; } }
This program shows method overriding. The greet method is called on parent, child, and parent reference to child object. The child method runs when the object is child.
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(); } }
The overriding method cannot have more restrictive access than the parent method. For example, if parent method is public, child method must be public too.
Static methods cannot be overridden, only instance methods can.
Constructors cannot be overridden.
Method overriding lets a child class provide its own version of a parent method.
The method signature must match exactly.
Use @Override to avoid mistakes.
Practice
Solution
Step 1: Understand method overriding signature rules
Method overriding requires the child method to have the exact same name and parameter list as the parent method.Step 2: Check return type and modifiers
The return type must be the same or a subtype, and the method cannot be static to override.Final Answer:
The method in the child class must have the same name and parameters as in the parent class. -> Option BQuick Check:
Method signature match = D [OK]
- Thinking return type can be different
- Assuming static methods can be overridden
- Changing parameter count in child method
public int calculate(int x)?Solution
Step 1: Match method signature exactly
The overriding method must have the same name and parameter types:calculate(int x).Step 2: Check return type and modifiers
Return type must beintand method must not be static.Final Answer:
public int calculate(int x) { return x * 2; } -> Option AQuick Check:
Exact signature and return type = A [OK]
- Changing return type to void
- Changing parameter type
- Making method static
class Parent {
void show() { System.out.println("Parent"); }
}
class Child extends Parent {
@Override
void show() { System.out.println("Child"); }
}
public class Test {
public static void main(String[] args) {
Parent obj = new Child();
obj.show();
}
}Solution
Step 1: Understand dynamic method dispatch
When a parent reference points to a child object, the overridden child method is called at runtime.Step 2: Check method overriding and call
Theshow()method is overridden in Child, soobj.show()calls Child's version.Final Answer:
Child -> Option DQuick Check:
Overridden method called at runtime = B [OK]
- Expecting parent method output
- Confusing compile-time and runtime method calls
- Ignoring @Override annotation effect
class Parent {
void display() {}
}
class Child extends Parent {
@Override
void display(int x) {}
}Solution
Step 1: Compare method signatures in Parent and Child
Parent hasdisplay()with no parameters; Child hasdisplay(int x)with one parameter.Step 2: Understand @Override annotation rules
@Override requires exact signature match; here, parameters differ, so it's not overriding.Final Answer:
Method display(int x) does not override display() due to different parameters. -> Option CQuick Check:
@Override requires exact signature match = C [OK]
- Ignoring parameter difference
- Thinking @Override can be used on any method
- Assuming method overloading is overriding
class Animal {
Number getValue() { return 10; }
}
class Dog extends Animal {
@Override
Integer getValue() { return 20; }
}Which statement about this overriding is correct?
Solution
Step 1: Check return types in parent and child methods
Parent returnsNumber, child returnsInteger, which is a subclass of Number.Step 2: Understand covariant return types in Java overriding
Java allows child methods to return a subtype of the parent's return type when overriding.Final Answer:
This is valid because Integer is a subclass of Number (covariant return type). -> Option AQuick Check:
Covariant return types allowed = A [OK]
- Thinking return types must be exactly the same
- Assuming @Override forbids different return types
- Confusing overloading with overriding
