-
Notifications
You must be signed in to change notification settings - Fork 0
/
rectangleInherit.java
38 lines (34 loc) · 1.24 KB
/
rectangleInherit.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Class Rectangle
class Rectangle{
int length;
int width;
Rectangle(int l, int w) // constructor where we are initializing values of length and breadth
{
this.length = l;
this.width = w;
}
int area() // area function to calculate the area of rectangle
{
return length * width;
}
}
class Cuboid extends Rectangle { // Class cuboid which is inheriting the class rectangle
int height;
Cuboid(int w, int l, int h) // constructor here also we are initializing the values of w,l,h
{
super(l, w); // super function which is calling the constructor of the parent class which is rectangle
this.height = h;
}
int volume() // class rectangle to calculate the volume of the cuboid
{
return length * width * height;
}
}
public class rectangleInherit{
public static void main(String[] args) {
Rectangle rec = new Rectangle(25, 25); // initializing the object for class rectangle
System.out.println(rec.area());
Cuboid cuboid = new Cuboid(20, 20, 20); // initializing the object of the class cuboid
System.out.println(cuboid.volume());
}
}