Commit 84adc2e
committed
feat: Add ShapeVIMP abstract class with CircleVIMP and Rectangle implementations
WHAT the code does:
Defines abstract class ShapeVIMP with two abstract methods:
- peimeterIMP(): calculates perimeter.
- areaIMP(): calculates area.
Defines CircleVIMP extending ShapeVIMP:
- Implements peimeterIMP() as 2πr.
- Implements areaIMP() as πr².
Defines Rectangle extending ShapeVIMP:
- Implements peimeterIMP() as 2 × (length + breadth).
- Implements areaIMP() as length × breadth.
Defines AbstractIMP with main():
- Creates a Rectangle, sets length=10 and breadth=5.
- Assigns Rectangle to a ShapeVIMP reference.
- Calls areaIMP() via the abstract reference and prints the result (50.0).
WHY this matters:
Demonstrates **abstraction**: ShapeVIMP defines a contract for all shapes (area and perimeter).
Shows **polymorphism**: a ShapeVIMP reference can point to different shape objects (Rectangle, CircleVIMP).
Encourages code reuse and flexibility by separating "what a shape does" from "how each shape does it".
HOW it works:
ShapeVIMP s = r (where r is a Rectangle).
- Calls areaIMP() → resolves to Rectangle.areaIMP() → computes 10 × 5 = 50.
Same approach could be used for CircleVIMP by setting radiusIMP and calling its methods.
Tips and gotchas:
Method names should follow Java naming conventions (perimeter() instead of peimeterIMP()).
Fields (radiusIMP, length, breadth) should be private with constructors or setters for encapsulation.
Currently only Rectangle is demonstrated; adding a CircleVIMP demo in main would show polymorphism more clearly.
Abstract class ShapeVIMP could be refactored into an interface if only abstract methods are required.
Use-cases:
Educational example of abstract classes and polymorphism in OOP.
Foundation for building extensible geometry libraries.
Practical demonstration of how base contracts ensure consistency across different subclasses.
Short key: class-shapevimp circle rectangle abstract-polymorphism.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 6741c43 commit 84adc2e
1 file changed
+11
-18
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | 1 | | |
2 | | - | |
3 | | - | |
| 2 | + | |
| 3 | + | |
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
8 | | - | |
9 | | - | |
| 8 | + | |
10 | 9 | | |
11 | 10 | | |
12 | | - | |
13 | | - | |
| 11 | + | |
14 | 12 | | |
15 | 13 | | |
16 | 14 | | |
17 | | - | |
18 | | - | |
| 15 | + | |
19 | 16 | | |
20 | 17 | | |
21 | 18 | | |
22 | 19 | | |
23 | | - | |
24 | | - | |
| 20 | + | |
25 | 21 | | |
26 | 22 | | |
27 | 23 | | |
28 | | - | |
29 | | - | |
| 24 | + | |
30 | 25 | | |
31 | 26 | | |
32 | | - | |
33 | | - | |
| 27 | + | |
| 28 | + | |
34 | 29 | | |
35 | 30 | | |
36 | 31 | | |
37 | 32 | | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
| 33 | + | |
| 34 | + | |
42 | 35 | | |
43 | 36 | | |
44 | 37 | | |
| |||
0 commit comments