-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathHybridInheritance.java
More file actions
62 lines (49 loc) · 1.56 KB
/
HybridInheritance.java
File metadata and controls
62 lines (49 loc) · 1.56 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
package code;
class Shapes {
public void area() {
System.out.println("Displays Area of Shape");
}
}
// Interface for color functionality
interface Colorable {
void setColor(String color);
String getColor();
}
// Derived class Triangle from Shape
class Triangles extends Shapes{
// Method to calculate area of a triangle
public void area(int h, int b) {
System.out.println("Triangle Area: " + (0.5) * b * h);
}
}
// Further derived class ColoredTriangle from Triangle
class ColoredTriangle extends Triangles implements Colorable {
private String color;
// Method to set the color
@Override
public void setColor(String color) {
this.color = color;
}
// Method to get the color
@Override
public String getColor() {
return color;
}
// Method to calculate area of the colored triangle
public void area() {
double area = (Math.sqrt(3) / 4) * (2 * 2); // Example with side = 2
System.out.println("Colored Triangle Area: " + area);
}
}
public class HybridInheritance {
public static void main(String[] args) {
Shapes s1 = new Shapes();
s1.area();
Triangles t1 = new Triangles();
t1.area(12, 2); // Calls Triangle's area method
ColoredTriangle ct = new ColoredTriangle();
ct.setColor("Blue"); // Set color
System.out.println("Colored Triangle color: " + ct.getColor());
ct.area(); // Calls ColoredTriangle's area method
}
}