-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathAbstractAreas.java
More file actions
60 lines (42 loc) · 1.19 KB
/
AbstractAreas.java
File metadata and controls
60 lines (42 loc) · 1.19 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
package chapter8;
abstract class Figure2 {
double dim1;
double dim2;
Figure2(double a, double b) {
dim1 = a;
dim2 = b;
}
abstract double area();
}
class Rectangle2 extends Figure2 {
Rectangle2(double a, double b) {
super(a, b);
}
//override area of Rectangle
double area() {
System.out.println("Inside area of Rectangle");
return dim1 * dim2;
}
}
class Triangle2 extends Figure2 {
Triangle2(double a, double b) {
super(a, b);
}
//override area of Triangle
double area() {
System.out.println("Inside area of Triangle");
return dim1 * dim2 / 2;
}
}
public class AbstractAreas {
public static void main(String[] args) {
//Figure2 f= new Figure2(); Illegal Now //Abstract classes can not be initiated with keyword new..
Rectangle2 r = new Rectangle2(9, 5);
Triangle2 t = new Triangle2(5, 8);
Figure2 figref; //This is ok , no object is created.. (it is a reference variable not an object)
figref =r;
System.out.println("Area is : "+figref.area());
figref =t;
System.out.println("Area is : "+figref.area());
}
}