Understanding Method Overriding in Java

public class OOPDemo { public static void main(String[] args) { Student stu = new Student(); // Reference variable of parent type pointing to child object Person stu2 = new Student();

    // Static method behavior
    stu.read(); // Output: I am overriding the parent class method
    stu2.read(); // Output: I am reading (parent's static method)
    
    // Instance method behavior - overriding takes effect
    stu.fn(); // Output: This is the subclass instance method
    stu2.fn(); // Output: This is the subclass instance method
    
    // Field access depends on the reference type
    // When subclass has no field with the same name, parent's field is used
    // When subclass has same-named field, the reference type determines which field is accessed
    System.out.println(stu.name + "--" + stu2.name);
}

}

// Student inherits from Person class Student extends Person { // Static methods in parent must be overridden with static methods in child public static void read() { System.out.println("I am overriding the parent class method"); }

public void fn() {
    System.out.println("This is the subclass instance method");
}

}

// Person class class Person { String name = "ParentName";

public Person() {
    System.out.println("Is parent constructor called?");
}

public static void read() {
    System.out.println("I am reading");
}

public void fn() {
    System.out.println("This is the parent class instance method");
}

}


<div>**Key Takeaway:** Static methods are resolved based on the reference type at compile time, while instance methods are resolved based on the actual object type at runtime through dynamic dispatch. </div>### Important Rules for Method Overriding

#### 1. Declaration Requirements

Method overriding applies specifically to inheritance hierarchies. The overriding method must have the same signature as the method in the parent class: identical return type, parameter list, and method name. Any difference in these elements results in method overloading rather than overriding.

#### 2. Access Modifier Restrictions (Liskov Substitution Principle)

The overriding method cannot have a more restrictive access modifier than the original method in the parent class. The subclass method must be at least as accessible as the parent method.

public class ComputingDevice { // Package-private (default) access int calculate(int value) { this.notifyUser(); System.out.println("Base calculation performed"); return 6; }

void notifyUser() {
    System.out.println("Notification sent");
}

}

public class LenovoBrand extends ComputingDevice { // This will cause a compilation error // Private is more restrictive than default/package-private private int calculate(int value) { this.notifyUser(); System.out.println("Lenovo brand calculation"); return 6; } }

public class Application { public static void main(String[] args) { ComputingDevice device = new LenovoBrand(); // This presents a problem because private methods cannot be invoked // from outside the class. To prevent runtime errors where code that // previously worked stops functioning after method overriding, // Java's design prevents this scenario altogether. device.calculate(4); } }


The acces level hierarchy from most to least restritcive is: private, default/package-private, protected, public. An overriding method can increase visibility (for example, from protected to public) but cannot decrease it.

</div>

Tags: java method-overriding Inheritance Polymorphism liskov-substitution-principle

Posted on Fri, 08 May 2026 18:11:37 +0000 by BenGilbert