-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstract.java
More file actions
87 lines (73 loc) · 1.66 KB
/
Abstract.java
File metadata and controls
87 lines (73 loc) · 1.66 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
76
77
78
79
80
81
82
83
84
85
86
package ABC;
import java.util.Scanner;
abstract class Shape{
//this is an abstract class so, there will be no object of this class.
double dim1;
double dim2;
public Shape(double a , double b)
{
dim1=a;// values passed from main method through a and b arguments.
dim2=b;
}
abstract double area();// this is an abstract method which will be defined in the subclass.
}
class Circle extends Shape{
double radius;
Circle(double a, double b, double r)
{
super(a,b);// accessing the Shape constructor.
radius=r;
}
void show()
{
System.out.println("The radius of the circle is :"+radius);
}
double area()//overriding the method area for circle.
{
System.out.println("The area of the circle is : ");
return(3.14*radius*radius);// we are not using 'r' here but using radius cause r passing values from to here in radius.
}
}
class Rectangle extends Shape{
Rectangle (double a, double b)
{
super(a,b);
}
void show()
{
System.out.println("The values of rectangle are: "+dim1+" & "+dim2);
}
double area()
{
System.out.println("The area of the rectangle is :");
return(dim1*dim2);
}
}
class Square extends Shape{
Square(double a, double b )
{
super (a,b);
}
void show()
{
System.out.println("The value of side of a square is :"+dim1);
}
double area()
{
System.out.println("The area of the rectangle is :");
return(dim1*dim2);
}
}
public class Test {
public static void main(String[] args) {
Circle c = new Circle(0,0,5);
Rectangle r = new Rectangle(5,10);
Square s = new Square(10,10);
System.out.println(c.area());
c.show();
System.out.println(r.area());
r.show();
System.out.println(s.area());
s.show();
}
}