Skip to content

Commit d969131

Browse files
committed
feat: demonstrate method overriding with @OverRide in abstract and concrete classes
WHAT was added: - Created abstract class `Parent1` with an abstract method `display()`. - Implemented `Child2` class that extends `Parent1` and provides its own `display()` method. - Created regular class `Parent` with a concrete `display()` method. - Implemented `Child` class that extends `Parent` and overrides the `display()` method. - Added `Main` class to test both abstract and regular inheritance scenarios. KEY LEARNINGS: 1. Abstract Class & Abstract Method: - Abstract classes can contain abstract methods (only signature, no body). - Any subclass must implement all abstract methods. - Example: `Parent1` → `Child2` implements `display()`. 2. @OverRide Annotation: - Ensures the child method correctly overrides the parent method. - If the method signature mismatches (e.g., typo), the compiler throws an error. - Improves code readability and prevents silent bugs. 3. Concrete Parent with Overridable Methods: - Normal classes can define methods that subclasses may override. - Example: `Parent.display()` → overridden by `Child.display()`. 4. Polymorphism (Dynamic Method Dispatch): - Reference type is parent, but actual object type decides which method executes. - `Parent1 obj1 = new Child2();` → Calls Child2’s implementation. - `Parent obj2 = new Child();` → Calls Child’s implementation. REAL-WORLD APPLICATIONS: - ✅ Framework Design: Abstract classes define contracts, subclasses provide concrete implementations. - ✅ API Evolution: Base classes provide default implementations, subclasses override where needed. - ✅ Polymorphism in OOP: Enables writing flexible, reusable, and extensible code. - ✅ Error Prevention: @OverRide guarantees correctness in method overriding. RULE OF THUMB: - Use abstract classes when you want to enforce a contract but also share partial implementation. - Always use `@Override` when overriding methods for safety and clarity. - Override concrete methods only if the subclass requires different behavior. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent 407d87a commit d969131

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Abstract Parent class with an abstract method
2+
abstract class Parent1 {
3+
// Abstract method (must be implemented by child classes)
4+
abstract public void display();
5+
}
6+
7+
// Child class that overrides the abstract method
8+
class Child2 extends Parent1 {
9+
@Override
10+
public void display() {
11+
System.out.println("Child2 implementation of display()");
12+
}
13+
}
14+
15+
// Regular Parent class with a concrete method
16+
class Parent {
17+
public void display() {
18+
System.out.println("Parent display method");
19+
}
20+
}
21+
22+
// Child class that overrides the display() method
23+
class Child extends Parent {
24+
@Override
25+
public void display() {
26+
System.out.println("Child implementation of display()");
27+
}
28+
}
29+
30+
// Main class to test
31+
public class Main {
32+
public static void main(String[] args) {
33+
// Test abstract inheritance
34+
Parent1 obj1 = new Child2();
35+
obj1.display();
36+
37+
// Test regular inheritance
38+
Parent obj2 = new Child();
39+
obj2.display();
40+
}
41+
}
42+
43+
/*
44+
1. abstract class + abstract method
45+
- Abstract class ke andar abstract method declare hota hai (sirf signature, body nahi hoti).
46+
- Har child class ko us abstract method ko implement karna mandatory hota hai.
47+
- Example: Parent1 ke andar `display()` abstract hai → Child2 ne usko override karke body di.
48+
49+
2. @Override annotation
50+
- Jab child class parent class ke method ko override karti hai,
51+
tab @Override lagane se compiler check karega ki method correctly override hua hai ya nahi.
52+
- Agar signature galat likha, to compiler error dega.
53+
- Isse bugs avoid hote hain.
54+
55+
3. Concrete Parent class
56+
- Agar parent class ke method already defined hain (non-abstract),
57+
to child class us method ko override kar sakti hai.
58+
- Example: Parent class ka `display()` → Child ne apna `display()` implement kiya.
59+
60+
4. Polymorphism in action
61+
- `Parent1 obj1 = new Child2();` → Abstract parent reference, but runtime pe Child2 ka method call hoga.
62+
- `Parent obj2 = new Child();` → Normal parent reference, runtime pe Child ka method call hoga.
63+
- Ye hi runtime polymorphism / dynamic method dispatch ka example hai.
64+
65+
5. Key Point:
66+
- Abstract method override karna mandatory hai.
67+
- Normal method override karna optional hai (agar behavior change karna ho to).
68+
- @Override annotation lagana best practice hai.
69+
*/

0 commit comments

Comments
 (0)