Hierarchical Inheritance in Java
Hierarchical inheritance means one parent (base) class has multiple child (derived) classes.
Each child class inherits the properties and methods of the parent class, but they can also have their own unique features.
Code Snippet
// Hierarchical Inheritance in Java
class shape {
float length, breadth, radius;
}
class rect extends shape {
public rect(float l, float b) {
length = l;
breadth = b;
}
float rectangle_area() {
return length * breadth;
}
}
class circle extends shape {
public circle(float r) {
radius = r;
}
float circle_area() {
return 3.14f * (radius * radius);
}
}
class square extends shape {
public square(float l) {
length = l;
}
float square_area() {
return (length * length);
}
}
public class App {
public static void main(String[] args) {
rect o1 = new rect(2, 5);
System.out.println("Area of Rectangle : " + o1.rectangle_area());
circle o2 = new circle(5);
System.out.println("Area of Circle : " + o2.circle_area());
square o3 = new square(3);
System.out.println("Area of Square : " + o3.square_area());
}
}