Commit 6d7d86b
committed
feat: Add CircleDemo and CylinderDemo to demonstrate inheritance and geometry methods
WHAT the code does:
Defines CircleDemo with:
- radius field.
- area(): computes circle area (πr²).
- perimeter(): computes circle perimeter (2πr).
- circumference(): delegates to perimeter().
Defines CylinderDemo extending CircleDemo with:
- height field.
- volume(): computes cylinder volume using area() × height.
Defines Inheritance with main():
- Creates CylinderDemo object.
- Assigns radius = 7 and height = 10.
- Prints cylinder volume and circle area.
WHY this matters:
Demonstrates **inheritance** in Java:
- CylinderDemo reuses CircleDemo’s area() for its volume calculation.
- Highlights code reuse: circle properties directly extend into cylinder computations.
Shows how subclass methods can build on superclass methods.
Provides a practical geometry-based example of inheritance.
HOW it works:
CylinderDemo inherits radius and area() from CircleDemo.
c.radius = 7, c.height = 10.
volume() = area() × height = (π × 7²) × 10 = 1539.38.
area() = π × 7² = 153.94.
Tips and gotchas:
Fields radius and height are public, breaking encapsulation—prefer private with getters/setters.
circumference() is redundant since it directly calls perimeter(); the two could be merged or one removed.
Class names should follow convention: Inheritance could be renamed GeometryInheritance for clarity.
Inheritance here models a “is-a” relationship, but mathematically, a cylinder is not a circle—it has-a circle base. Composition would be more semantically correct.
Use-cases:
Educational demonstration of inheritance and method reuse.
Geometry-based examples for teaching OOP principles.
Foundation for extending into more advanced shape hierarchies (e.g., abstract Shape or interface).
Short key: class-circledemo cylinderdemo inheritance geometry.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 2d76078 commit 6d7d86b
1 file changed
+9
-19
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
| 1 | + | |
3 | 2 | | |
4 | 3 | | |
5 | | - | |
6 | | - | |
| 4 | + | |
7 | 5 | | |
8 | 6 | | |
9 | | - | |
10 | | - | |
11 | | - | |
| 7 | + | |
12 | 8 | | |
13 | 9 | | |
14 | | - | |
15 | | - | |
| 10 | + | |
16 | 11 | | |
17 | 12 | | |
18 | 13 | | |
19 | 14 | | |
20 | | - | |
21 | | - | |
| 15 | + | |
22 | 16 | | |
23 | 17 | | |
24 | | - | |
25 | | - | |
| 18 | + | |
26 | 19 | | |
27 | 20 | | |
28 | 21 | | |
29 | 22 | | |
30 | 23 | | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
| 24 | + | |
| 25 | + | |
35 | 26 | | |
36 | | - | |
37 | 27 | | |
38 | 28 | | |
39 | 29 | | |
40 | 30 | | |
41 | 31 | | |
42 | 32 | | |
43 | | - | |
| 33 | + | |
0 commit comments