-
Notifications
You must be signed in to change notification settings - Fork 0
/
abstract_keyword.java
43 lines (39 loc) · 1.01 KB
/
abstract_keyword.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
package src.inheritance;
abstract class figure1 {
double dim1, dim2;
figure1(double a, double b) {
dim1 = a;
dim2 = b;
}
abstract double area();
}
class rectangle1 extends figure1 {
rectangle1(double a, double b) {
super(a, b);
}
double area() {
System.out.println("Rectangle area:");
return dim1 * dim2;
}
}
class triangle1 extends figure1 {
triangle1(double a, double b) {
super(a, b);
}
double area() {
System.out.println("Triangle area:");
return (dim1 * dim2) / 2;
}
}
class absDemo {
public static void main(String[] args) {
// figure o1 = new figure(5, 3); //this is illegal cause abstract is hidden
rectangle1 o2 = new rectangle1(8, 10);
triangle1 o3 = new triangle1(21, 12);
figure1 fig; //correct! cause there's no object
fig = o2;
System.out.println("Area is " + fig.area());
fig = o3;
System.out.println("Area is " + fig.area());
}
}