-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram#D.java
More file actions
41 lines (38 loc) · 948 Bytes
/
Program#D.java
File metadata and controls
41 lines (38 loc) · 948 Bytes
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
/* Create a class Shape with a method area(). Create subclasses Circle and Rectangle that override the area() method to calculate the area of their respective shapes.
Area of Circle: 28.26
-------------------
Area of Rectangle:12 */
class Main {
public static void main(String [] args)
{ Circle s1 = new Circle(3);
Rectangle s2 = new Rectangle(3,4);
s1.area();
s2.area();
}
}
class Shape {
void area(){
System.out.println("Area");
}
}
class Circle extends Shape {
int r;
Circle(int r)
{
this.r=r;
}
void area(){
double result=3.14*r*r;
System.out.printf("Area of Circle: %.2f\n", result);
System.out.println("-------------------"); }
}
class Rectangle extends Shape {
int l,b;
Rectangle(int l,int b){
this.l=l;
this.b=b;
}
void area(){
System.out.println("Area of Rectangle:"+(l*b));
}
}