Method Overriding
Redefining parent class methods
Interview Relevant: Overriding vs Overloading
6 min read
Method Overriding in Java
Method overriding allows a subclass to provide a specific implementation of a method already defined in its parent class.
Overriding Rules
- Same method name and parameter list
- Return type: same or covariant (subtype)
- Access: same or more permissive (not less)
- Cannot override static, final, or private methods
- Exceptions: same, subtype, or fewer checked exceptions
🔑 Interview Tip: Overriding = Runtime polymorphism (same signature, different class). Overloading = Compile-time (different parameters, same class).
💡 Best Practice: Always use @Override annotation. It catches errors at compile time if signature doesn't match!
Code Examples
Basic method overriding
java
1// Basic Method Overriding
2public class Animal {
3 public void makeSound() {
4 System.out.println("Some generic animal sound");
5 }
6
7 public void eat() {
8 System.out.println("Animal is eating");
9 }
10}
11
12public class Dog extends Animal {
13 @Override // ALWAYS use this annotation!
14 public void makeSound() {
15 System.out.println("Woof! Woof!");
16 }
17
18 // eat() is inherited as-is
19}
20
21public class Cat extends Animal {
22 @Override
23 public void makeSound() {
24 System.out.println("Meow!");
25 }
26}
27
28// Runtime polymorphism in action
29Animal[] animals = {new Dog(), new Cat(), new Animal()};
30for (Animal a : animals) {
31 a.makeSound();
32}
33// Output:
34// Woof! Woof!
35// Meow!
36// Some generic animal soundWhat can and cannot be overridden
java
1// Override Rules Demonstration
2public class Parent {
3 // Can be overridden
4 public void publicMethod() {}
5 protected void protectedMethod() {}
6 void defaultMethod() {}
7
8 // CANNOT be overridden
9 private void privateMethod() {} // Not visible to child
10 public final void finalMethod() {} // final prevents override
11 public static void staticMethod() {} // Static is hidden, not overridden
12}
13
14public class Child extends Parent {
15 @Override
16 public void publicMethod() {} // ✓ OK
17
18 @Override
19 protected void protectedMethod() {} // ✓ OK
20
21 @Override
22 void defaultMethod() {} // ✓ OK (same package)
23
24 // ✗ These would cause errors:
25 // @Override public void privateMethod() {} // Not overriding!
26 // @Override public void finalMethod() {} // Cannot override final
27
28 // This HIDES, not overrides (no @Override!)
29 public static void staticMethod() {} // Different method
30}Access modifiers and return type rules
java
1// Access and Return Type Rules
2public class Parent {
3 protected Number getValue() {
4 return 10;
5 }
6
7 public void display() {}
8}
9
10public class Child extends Parent {
11 // Covariant return type - can return subtype
12 @Override
13 public Integer getValue() { // Integer is subtype of Number
14 return 20;
15 }
16
17 // Access can be same or MORE permissive
18 @Override
19 public void display() {} // protected → public OK
20
21 // ✗ Cannot be more restrictive:
22 // @Override protected void display() {} // ERROR if parent was public
23}
24
25// Exception Rules
26public class ParentEx {
27 public void doSomething() throws IOException {
28 // ...
29 }
30}
31
32public class ChildEx extends ParentEx {
33 // Can throw same exception
34 @Override
35 public void doSomething() throws IOException {}
36
37 // Or subclass exception
38 // @Override public void doSomething() throws FileNotFoundException {}
39
40 // Or no exception at all
41 // @Override public void doSomething() {}
42
43 // ✗ Cannot throw broader exception
44 // @Override public void doSomething() throws Exception {} // ERROR!
45}Overriding vs Overloading comparison
java
1// Overriding vs Overloading - Interview Classic!
2
3// OVERLOADING - Same class, different parameters
4public class Calculator {
5 public int add(int a, int b) { return a + b; }
6 public int add(int a, int b, int c) { return a + b + c; } // Overload
7 public double add(double a, double b) { return a + b; } // Overload
8}
9
10// OVERRIDING - Different class, same signature
11public class Shape {
12 public double area() { return 0; }
13}
14
15public class Circle extends Shape {
16 private double radius;
17
18 @Override // Same signature, different implementation
19 public double area() {
20 return Math.PI * radius * radius;
21 }
22}
23
24// Key Differences Table:
25// | Aspect | Overloading | Overriding |
26// |---------------|----------------------|----------------------|
27// | Classes | Same class | Parent-Child |
28// | Parameters | Must be different | Must be same |
29// | Return type | Can be different | Same or covariant |
30// | Binding | Compile-time | Runtime |
31// | Purpose | Convenience | Specialization |Use Cases
- Customizing inherited behavior
- Runtime polymorphism
- Template method pattern
- toString(), equals(), hashCode()
- Listener/callback implementations
- Framework extension points
Common Mistakes to Avoid
- Forgetting @Override annotation
- Wrong method signature (overloading instead)
- Making access more restrictive
- Throwing broader exceptions
- Trying to override final/static/private
- Confusing with overloading