Commit 798012f
committed
feat: Add abstract class Test1 with concrete subclass Test2 demonstrating abstraction
WHAT the code does:
Defines abstract class Test1 with:
- abstract method show() (no implementation).
- concrete method display() that prints "Hello World!".
Defines Test2 extending Test1 with:
- Implementation of show() that prints "Welcome".
Defines Abs1 with main():
- Instantiates Test2.
- Calls display() (inherited from Test1) and show() (implemented in Test2).
WHY this matters:
Demonstrates **abstraction** in Java:
- Abstract classes can define both abstract methods (enforcing subclass implementation) and concrete methods (shared behavior).
Shows how subclasses must implement all abstract methods of their parent.
Illustrates code reuse: Test2 inherits display() while providing its own logic for show().
HOW it works:
Test2 t = new Test2();
- t.display() → calls Test1’s concrete method → prints "Hello World!".
- t.show() → calls Test2’s implementation → prints "Welcome".
Tips and gotchas:
Abstract classes cannot be instantiated directly; only concrete subclasses can be created.
@ Override annotation should be added in Test2.show() for clarity and compiler checking.
Class names could be more descriptive: Abs1 → AbstractDemo for readability.
Abstract classes differ from interfaces: they can hold concrete methods and state, not just abstract declarations.
Use-cases:
Educational demo of abstraction in OOP.
Foundation for designing base classes that enforce method contracts while sharing reusable logic.
Practical in frameworks where base class provides template methods and subclasses provide specific implementations.
Short key: class-test1 test2 abstraction abstract-method.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 6aa3d7f commit 798012f
1 file changed
+9
-14
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
| 1 | + | |
3 | 2 | | |
4 | | - | |
5 | | - | |
6 | | - | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
7 | 6 | | |
8 | 7 | | |
9 | 8 | | |
10 | | - | |
11 | | - | |
12 | | - | |
13 | | - | |
| 9 | + | |
| 10 | + | |
14 | 11 | | |
15 | 12 | | |
16 | 13 | | |
17 | 14 | | |
18 | | - | |
19 | | - | |
20 | | - | |
21 | | - | |
| 15 | + | |
| 16 | + | |
22 | 17 | | |
23 | 18 | | |
24 | 19 | | |
25 | 20 | | |
26 | | - | |
| 21 | + | |
0 commit comments