Skip to content

Commit 798012f

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

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed
Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,21 @@
1-
abstract class Test1
2-
{
1+
abstract class Test1 {
32
abstract void show();
4-
void display()
5-
{
6-
System.out.println("Hello Woeld!");
3+
4+
void display() {
5+
System.out.println("Hello World!");
76
}
87
}
98

10-
class Test2 extends Test1
11-
{
12-
void show()
13-
{
9+
class Test2 extends Test1 {
10+
void show() {
1411
System.out.println("Welcome");
1512
}
1613
}
1714

18-
class Abs1
19-
{
20-
public static void main(String[] args)
21-
{
15+
class Abs1 {
16+
public static void main(String[] args) {
2217
Test2 t = new Test2();
2318
t.display();
2419
t.show();
2520
}
26-
}
21+
}

0 commit comments

Comments
 (0)