-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestAbstract.java
More file actions
75 lines (71 loc) · 1.37 KB
/
TestAbstract.java
File metadata and controls
75 lines (71 loc) · 1.37 KB
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
abstract class Shape
{
String color;
//Declaring two abstract methods
abstract double area();
public abstract String toString();
//Declaring Constructor
public Shape(String color)
{
System.out.println("Shape Constructor is Called");
this.color = color;
}
//Declaring a Concrete method
public String getColor()
{
return color;
}
}
class Circle extends Shape
{
double radius;
//Defining Constructor
public Circle (String color,double radius)
{
super(color);
System.out.println("Circle Constructor is Called");
this.radius = radius;
}
@Override
double area()
{
return Math.PI * Math.pow(radius,2);
}
@Override
public String toString()
{
return "Circle color is : "+super.color+" and area is : " + area();
}
}
class Rectangle extends Shape
{
double length;
double width;
public Rectangle(String color,int length,int width)
{
super(color);
System.out.println("Rectangle Constructor is Called");
this.length = length;
this.width = width;
}
@Override
double area()
{
return length*width;
}
@Override
public String toString()
{
return "Rectangle color is "+super.color + "and area is : " + area();
}
}
public class TestAbstract
{
public static void main(String [] args)
{
Shape s1 = new Circle("SkyBlue",2);
Shape s2 = new Rectangle("Green",5,10);
System.out.println(s1.toString());
System.out.println(s2.toString());
}
}