Hierarchical Inheritance
Hierarchical inheritance in Java is a type of inheritance where multiple subclasses inherit from a single superclass. This type of inheritance allows a single class to serve as a base class for multiple derived classes, promoting code reuse and consistency across related classes.
Example of Hierarchical Inheritance in Java
Let's create an example with a superclass called Animal and three subclasses: Dog, Cat, and Bird. Each subclass will inherit from the Animal superclass and add specific behavior.
// Superclass
class Animal {
String name;
// Constructor
Animal(String name) {
this.name = name;
}
// Method to display the name
void display() {
System.out.println("Animal Name: " + name);
}
// Method to make a sound (to be overridden by subclasses)
void sound() {
System.out.println("Animal makes a sound");
}
}
// Subclass Dog
class Dog extends Animal {
// Constructor
Dog(String name) {
super(name);
}
// Override the sound method
@Override
void sound() {
System.out.println("Dog barks");
}
}
// Subclass Cat
class Cat extends Animal {
// Constructor
Cat(String name) {
super(name);
}
// Override the sound method
@Override
void sound() {
System.out.println("Cat meows");
}
}
// Subclass Bird
class Bird extends Animal {
// Constructor
Bird(String name) {
super(name);
}
// Override the sound method
@Override
void sound() {
System.out.println("Bird chirps");
}
}
// Main class to test the hierarchical inheritance
public class Main {
public static void main(String[] args) {
// Create instances of each subclass
Dog dog = new Dog("Buddy");
Cat cat = new Cat("Whiskers");
Bird bird = new Bird("Tweety");
// Display information and sounds for each animal
dog.display();
dog.sound();
cat.display();
cat.sound();
bird.display();
bird.sound();
}
}
Program to Try
// Superclass
class Shape {
// Method to display shape information
void display() {
System.out.println("This is a shape.");
}
// Abstract method to calculate the area (to be overridden by subclasses)
double calculateArea() {
return 0;
}
}
// Subclass Rectangle
class Rectangle extends Shape {
double length;
double width;
// Constructor
Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
// Override the calculateArea method
@Override
double calculateArea() {
return length * width;
}
// Override the display method
@Override
void display() {
System.out.println("This is a rectangle.");
}
}
// Subclass Triangle
class Triangle extends Shape {
double base;
double height;
// Constructor
Triangle(double base, double height) {
this.base = base;
this.height = height;
}
// Override the calculateArea method
@Override
double calculateArea() {
return 0.5 * base * height;
}
// Override the display method
@Override
void display() {
System.out.println("This is a triangle.");
}
}
// Subclass Circle
class Circle extends Shape {
double radius;
// Constructor
Circle(double radius) {
this.radius = radius;
}
// Override the calculateArea method
@Override
double calculateArea() {
return Math.PI * radius * radius;
}
// Override the display method
@Override
void display() {
System.out.println("This is a circle.");
}
}
// Main class to test the hierarchical inheritance
public class Main {
public static void main(String[] args) {
// Create instances of each subclass
Rectangle rectangle = new Rectangle(5, 3);
Triangle triangle = new Triangle(4, 2.5);
Circle circle = new Circle(2.5);
// Display information and areas for each shape
System.out.println("Rectangle:");
rectangle.display();
System.out.println("Area: " + rectangle.calculateArea());
System.out.println("\nTriangle:");
triangle.display();
System.out.println("Area: " + triangle.calculateArea());
System.out.println("\nCircle:");
circle.display();
System.out.println("Area: " + circle.calculateArea());
}
}
Comments
Post a Comment