Commit 4acf344
committed
docs: Explain abstract class features, final methods, and abstract class hierarchies in Java
WHAT the update covers:
Adds detailed notes and examples explaining abstract classes in Java.
Clarifies that abstract classes can contain:
- Abstract methods (unimplemented).
- Concrete methods (implemented).
- Instance variables and constructors.
- Static members (methods and variables).
Demonstrates how abstract classes combine structure with flexibility by partially implementing functionality.
WHY this matters:
Abstract classes are a cornerstone of OOP in Java:
- Allow code reuse while enforcing a contract for subclasses.
- Provide a middle ground between fully implemented base classes and pure abstraction (interfaces).
Understanding when and how to use them is a common interview topic and critical for scalable design.
HOW it works:
Example Shape:
- Has a constructor and field (color).
- Declares abstract method calculateArea().
- Provides concrete method displayColor().
Example Vehicle:
- stopEngine() declared final → cannot be overridden by subclasses.
- Car implements startEngine() but cannot override stopEngine().
Example abstract hierarchy:
- Shape (abstract) → TwoDimensionalShape (abstract) → Circle (concrete).
- Circle implements calculateArea() and calculatePerimeter().
- Shows that abstract classes can extend other abstract classes, without being forced to implement all methods immediately.
Tips and gotchas:
Abstract classes cannot be instantiated directly.
Subclasses must implement all abstract methods unless they are also declared abstract.
Final methods in abstract classes ensure consistent behavior across all subclasses.
Before Java 8, only abstract classes could hold concrete methods; now interfaces can as well (default and static methods).
Prefer abstract classes when you need to share state (fields) and behavior, not just method signatures.
Use-cases:
Educational reference for interviews and practice.
Foundation for designing reusable class hierarchies (e.g., shapes, vehicles, payment systems).
Supports clear separation of concerns: abstract base for contracts, concrete subclasses for specific implementations.
Short key: docs-abstract-class methods final hierarchy.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 798012f commit 4acf344
1 file changed
+1
-5
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
| 2 | + | |
3 | 3 | | |
4 | 4 | | |
5 | | - | |
6 | 5 | | |
7 | 6 | | |
8 | 7 | | |
| |||
30 | 29 | | |
31 | 30 | | |
32 | 31 | | |
33 | | - | |
34 | 32 | | |
35 | 33 | | |
36 | 34 | | |
| |||
56 | 54 | | |
57 | 55 | | |
58 | 56 | | |
59 | | - | |
60 | 57 | | |
61 | 58 | | |
62 | 59 | | |
63 | 60 | | |
64 | 61 | | |
65 | 62 | | |
66 | 63 | | |
67 | | - | |
68 | 64 | | |
69 | 65 | | |
70 | 66 | | |
| |||
0 commit comments