-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcwh_52_practice_set_Inheritance.java
91 lines (81 loc) · 1.91 KB
/
cwh_52_practice_set_Inheritance.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package company;
// Q1 & Q3 :-
class Circle1
{
int radius;
Circle1(int r)
{
this.radius = r;
}
public double area()
{
return Math.PI*this.radius*this.radius;
}
}
class Cylinder1 extends Circle1
{
int height;
Cylinder1(int r, int h)
{
super(r);
this.height = h;
}
public double volume()
{
return Math.PI*this.radius*this.radius*this.height;
}
}
// Q2 & Q4 :-
class rectangle1
{
int length, breadth;
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public int getBreadth() {
return breadth;
}
public void setBreadth(int breadth) {
this.breadth = breadth;
}
public int area()
{
return this.length*this.breadth;
}
}
class Cuboid extends rectangle1
{
int height;
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public int volume()
{
return this.length*this.breadth*this.height;
}
}
public class cwh_52_practice_set_Inheritance
{
public static void main(String[] args)
{
// Question-1 --> Create a class Circle and use inheritance to create another class Cylinder from it.
// Question-3 --> Create methods for area and volume in Q1.
Cylinder1 cr1 = new Cylinder1(4,5);
System.out.println("\nArea of the circle is = " +cr1.area());
System.out.println("Volume of the cylinder is = " +cr1.volume());
// Question-2 --> Create a class Rectangle and use inheritance to create another class Cuboid. Try to keep it as close to real world scenario as possible.
// Question-4 --> Create methods for area and volume in Q2. Also create getters and setters.
Cuboid cb = new Cuboid();
cb.setLength(3);
cb.setBreadth(4);
cb.setHeight(5);
System.out.println("\nThe area of rectangle is = " +cb.area());
System.out.println("The volume of cuboid is = " +cb.volume());
}
}