Skip to content

Commit 6d7d86b

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

File tree

1 file changed

+9
-19
lines changed

1 file changed

+9
-19
lines changed
Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,33 @@
1-
class CircleDemo
2-
{
1+
class CircleDemo {
32
public double radius;
43

5-
public double area()
6-
{
4+
public double area() {
75
return Math.PI * radius *radius;
86
}
9-
10-
public double perimeter()
11-
{
7+
public double perimeter() {
128
return 2*Math.PI*radius;
139
}
14-
public double circumference()
15-
{
10+
public double circumference() {
1611
return perimeter();
1712
}
1813
}
1914

20-
class CylinderDemo extends CircleDemo
21-
{
15+
class CylinderDemo extends CircleDemo {
2216
public double height;
2317

24-
public double volume()
25-
{
18+
public double volume() {
2619
return area()*height;
2720
}
2821

2922
}
3023

31-
public class Inheritance
32-
{
33-
public static void main(String[] args)
34-
{
24+
public class Inheritance {
25+
public static void main(String[] args) {
3526
CylinderDemo c = new CylinderDemo();
36-
3727
c.radius = 7;
3828
c.height = 10;
3929

4030
System.out.println("Volume " + c.volume());
4131
System.out.println("Area " + c.area());
4232
}
43-
}
33+
}

0 commit comments

Comments
 (0)