Commit d969131
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
1 file changed
+69
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
0 commit comments