Skip to content

Commit 0c085ca

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

1 file changed

+12
-24
lines changed
Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,53 +1,41 @@
1-
//Need Practice the same thing again.
2-
3-
class RectangleDemo
4-
{
1+
class RectangleDemo {
52
int length;
63
int breadth;
74

8-
RectangleDemo()
9-
{
5+
RectangleDemo() {
106
length=breadth=1;
117
}
12-
RectangleDemo(int l,int b)
13-
{
8+
9+
RectangleDemo(int l,int b) {
1410
length=l;
1511
breadth=b;
1612
}
17-
1813
}
1914

20-
class Cuboid extends RectangleDemo
21-
{
15+
class Cuboid extends RectangleDemo {
2216
int height;
2317

24-
Cuboid()
25-
{
18+
Cuboid() {
2619
height=1;
2720
}
2821

29-
Cuboid(int h)
30-
{
22+
Cuboid(int h) {
3123
height=h;
3224
}
33-
Cuboid(int l,int b,int h)
34-
{
25+
26+
Cuboid(int l,int b,int h) {
3527
super(l,b);
3628
height=h;
3729
}
3830

39-
int volume()
40-
{
31+
int volume() {
4132
return length*breadth*height;
4233
}
4334
}
4435

45-
public class SuperClassParameterisedConstructors
46-
{
47-
public static void main(String[] args)
48-
{
36+
public class SuperClassParameterisedConstructors {
37+
public static void main(String[] args) {
4938
Cuboid c=new Cuboid(5,3,10);
5039
System.out.println(c.volume());
5140
}
5241
}
53-

0 commit comments

Comments
 (0)