Skip to content

Commit 4acf344

Browse files
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

File tree

1 file changed

+1
-5
lines changed

1 file changed

+1
-5
lines changed

Section13AbstractClasses/src/Abstract.md

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
An abstract class in Java can contain both abstract and concrete methods.
2-
This is one of the key features that distinguishes abstract classes from interfaces (prior to Java 8).
2+
This is one of the key features that distinguish abstract classes from interfaces (prior to Java 8).
33

44
An abstract class can have any of the following:
5-
65
1. Abstract methods (methods without a body)
76
2. Concrete methods (methods with a body)
87
3. Instance variables
@@ -30,7 +29,6 @@ abstract class Shape {
3029
}
3130
```
3231

33-
3432
Yes, an abstract class can contain final methods.
3533

3634
Final methods in Java are methods that cannot be overridden by subclasses.
@@ -56,15 +54,13 @@ class Car extends Vehicle {
5654
void startEngine() {
5755
System.out.println("Car engine started");
5856
}
59-
6057
// Cannot override stopEngine() as it's final in the superclass
6158
}
6259
```
6360

6461
Yes, an abstract class can extend another abstract class.
6562

6663
This is perfectly valid in Java and can be useful for creating a hierarchy of abstract classes,
67-
6864
each adding or refining abstract or concrete methods.
6965

7066
When an abstract class extends another abstract class, it is not required to implement the abstract methods of its

0 commit comments

Comments
 (0)