"Java Full Stack Learning: Rules of Method Overriding"

🚀 Day 53 / 180 — Java Full Stack Learning Topic: Rules of Method Overriding 🔹 1️⃣ Method Signature Must Be Same Signature = method name + parameter list (type, order, number) Return type not part of signature. ✅ Example: class Parent { void show(int a) { } } class Child extends Parent { void show(int a) { } // ✅ overriding } 🔹 2️⃣ Return Type Rule — Covariant Return Type Before Java 1.5 → return type must be exact same. After Java 1.5 → allowed to return subclass type (if return type is a class, not primitive). ✅ Example: class Animal {} class Dog extends Animal {} class Parent { Animal getAnimal() { return new Animal(); } } class Child extends Parent { Dog getAnimal() { return new Dog(); } // ✅ covariant return } ❌ Primitive types must match exactly. 🔹 3️⃣ Access Modifier Rule Can widen access, but cannot reduce it. Parent Modifier Allowed in Child public public only protected protected, public default (package) default, protected, public private cannot override ✅ Example: class Parent { protected void display() {} } class Child extends Parent { public void display() {} // ✅ wider } 🔹 4️⃣ Private Methods Cannot Be Overridden Private = not inherited. Same name in child = new method, not override. ✅ Example: class Parent { private void show() { System.out.println("Parent"); } } class Child extends Parent { void show() { System.out.println("Child"); } // new method } ➡️ @Override not allowed here. 🔹 5️⃣ Static Methods → Method Hiding, Not Overriding Static methods belong to class, not object. Both can have same signature, but method hiding occurs. ✅ Example: class Parent { static void show() { System.out.println("Parent"); } } class Child extends Parent { static void show() { System.out.println("Child"); } // Hiding } 🔹 6️⃣ Final Methods Cannot Be Overridden final → cannot change or override in subclass. final class → cannot be inherited. ✅ Example: class Parent { final void run() { System.out.println("Parent run"); } } class Child extends Parent { // void run() {} ❌ Error } 🔹 7️⃣ Exception Handling in Overriding If parent throws checked exception: Child may throw same or subclass exception. Or throw none. If parent doesn’t throw checked exception → child cannot throw new checked exception. ✅ Example: class Parent { void show() throws Exception {} } class Child extends Parent { void show() throws IOException {} // ✅ subclass } ❌ Invalid Example: class Parent { void show() {} } class Child extends Parent { void show() throws IOException {} // ❌ compile-time error } 🔹 8️⃣ Abstract Method Overriding Abstract parent → child must implement all abstract methods. Abstract subclass of normal class → can make a concrete method abstract again. ✅ Example: class Normal { void display() { System.out.println("Normal display"); } } abstract class AbstractChild extends Normal { abstract void display(); // ✅ allowed } #Java#JavaProgramming#JavaDeveloper

  • text, letter

To view or add a comment, sign in

Explore content categories