Commit 0c085ca
committed
feat: Add Cuboid class extending RectangleDemo with super constructor chaining
WHAT the code does:
Defines RectangleDemo with:
- No-arg constructor initializing length and breadth = 1.
- Parameterized constructor initializing length and breadth with given values.
Defines Cuboid extending RectangleDemo with:
- No-arg constructor setting height = 1.
- Single-parameter constructor setting height.
- Three-parameter constructor calling super(l,b) to initialize parent fields, then setting height.
- volume(): computes length × breadth × height.
Defines SuperClassParameterisedConstructors with main():
- Creates a Cuboid using three-parameter constructor (5,3,10).
- Prints volume.
WHY this matters:
Demonstrates **constructor chaining** in inheritance:
- Child class uses super(...) to initialize parent class fields.
Shows code reuse: Cuboid builds upon RectangleDemo’s logic rather than reimplementing.
Illustrates how subclass-specific behavior (volume) builds on superclass state (length × breadth).
HOW it works:
Cuboid c = new Cuboid(5,3,10):
- Calls RectangleDemo(int l,int b) → sets length=5, breadth=3.
- Sets height=10.
- volume() = 5 × 3 × 10 = 150.
Output: 150.
Tips and gotchas:
Fields are package-private; better practice is to make them private with getters/setters for encapsulation.
volume() currently uses raw multiplication; could be refactored to reuse an area() method from RectangleDemo.
Class names are descriptive, but SuperClassParameterisedConstructors could be renamed CuboidDemo for clarity.
No validation exists for negative dimensions; in production, constructors or setters should enforce positivity.
Use-cases:
Educational demonstration of super constructor usage and inheritance.
Geometry modeling example extending from 2D shapes (rectangle) to 3D solids (cuboid).
Foundation for creating reusable shape hierarchies in object-oriented design.
Short key: class-rectangledemo cuboid super-constructor chaining.
Signed-off-by: https://github.com/Someshdiwan <[email protected]>1 parent 4e63eb7 commit 0c085ca
File tree
1 file changed
+12
-24
lines changed- Section12Inheritance/src
1 file changed
+12
-24
lines changedLines changed: 12 additions & 24 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
2 | | - | |
3 | | - | |
4 | | - | |
| 1 | + | |
5 | 2 | | |
6 | 3 | | |
7 | 4 | | |
8 | | - | |
9 | | - | |
| 5 | + | |
10 | 6 | | |
11 | 7 | | |
12 | | - | |
13 | | - | |
| 8 | + | |
| 9 | + | |
14 | 10 | | |
15 | 11 | | |
16 | 12 | | |
17 | | - | |
18 | 13 | | |
19 | 14 | | |
20 | | - | |
21 | | - | |
| 15 | + | |
22 | 16 | | |
23 | 17 | | |
24 | | - | |
25 | | - | |
| 18 | + | |
26 | 19 | | |
27 | 20 | | |
28 | 21 | | |
29 | | - | |
30 | | - | |
| 22 | + | |
31 | 23 | | |
32 | 24 | | |
33 | | - | |
34 | | - | |
| 25 | + | |
| 26 | + | |
35 | 27 | | |
36 | 28 | | |
37 | 29 | | |
38 | 30 | | |
39 | | - | |
40 | | - | |
| 31 | + | |
41 | 32 | | |
42 | 33 | | |
43 | 34 | | |
44 | 35 | | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | | - | |
| 36 | + | |
| 37 | + | |
49 | 38 | | |
50 | 39 | | |
51 | 40 | | |
52 | 41 | | |
53 | | - | |
0 commit comments