// Interface to simulate Hybrid Inheritance
interface Printable {
void printDetails();
}
// Base class for Single, Multilevel and Hierarchical Inheritance
class Animal {
void eat() {
[Link]("Animal eats food.");
}
}
// Single Inheritance: Dog inherits Animal
class Dog extends Animal {
void bark() {
[Link]("Dog barks.");
}
}
// Multilevel Inheritance: Puppy inherits Dog
class Puppy extends Dog {
void weep() {
[Link]("Puppy weeps.");
}
}
// Hierarchical Inheritance: Cat also inherits Animal
class Cat extends Animal {
void meow() {
[Link]("Cat meows.");
}
}
// Hybrid Inheritance: Child inherits from Animal and implements Printable
class HybridChild extends Animal implements Printable {
public void play() {
[Link]("Hybrid child plays.");
}
public void printDetails() {
[Link]("Details printed from Printable interface.");
}
}// Main class to demonstrate all types
public class InheritanceExample {
public static void main(String[] args) {
[Link]("=== Single Inheritance ===");
Dog dog = new Dog();
[Link](); // from Animal
[Link](); // from Dog
[Link]("\n=== Multilevel Inheritance ===");
Puppy puppy = new Puppy();
[Link](); // from Animal
[Link](); // from Dog
[Link](); // from Puppy
[Link]("\n=== Hierarchical Inheritance ===");
Cat cat = new Cat();
[Link](); // from Animal
[Link](); // from Cat
[Link]("\n=== Hybrid Inheritance ===");
HybridChild hc = new HybridChild();
[Link](); // from Animal
[Link](); // from HybridChild
[Link](); // from Printable interface
}
}
// Main class
import [Link];
public class AbstractShapeTest {
public static void main(String[] args) {
Shape shape;
Scanner scanner = new Scanner([Link]);
int choice;
do {
[Link]("\nChoose a Shape to Calculate Area:");
[Link]("1. Rectangle");
[Link]("2. Circle");
[Link]("3. Triangle");
[Link]("4. Exit");
[Link]("Enter your choice: ");
choice = [Link]();
switch (choice) {
case 1:
shape = new Rectangle();
[Link]();
break;
case 2:
shape = new Circle();
[Link]();
break;
case 3:
shape = new Triangle();
[Link]();
break;
case 4:
[Link]("Exiting program...");
break;
default:
[Link]("Invalid choice! Try again.");
}
} while (choice != 4);
[Link]();
}
}
// Abstract class
abstract class Shape {
int length, breadth, radius;
Scanner input = new Scanner([Link]);
// Abstract method
abstract void printArea();
}
// Rectangle subclass
class Rectangle extends Shape {
void printArea() {
[Link]("\n*** Rectangle Area ***");
[Link]("Enter length: ");
length = [Link]();
[Link]("Enter breadth: ");
breadth = [Link]();
[Link]("Area of Rectangle: " + (length * breadth));
}
}
// Circle subclass
class Circle extends Shape {
void printArea() {
[Link]("\n*** Circle Area ***");
[Link]("Enter radius: ");
radius = [Link]();
[Link]("Area of Circle: " + ([Link] * radius * radius));
}
}
// Triangle subclass
class Triangle extends Shape {
void printArea() {
[Link]("\n*** Triangle Area ***");
[Link]("Enter base: ");
length = [Link]();
[Link]("Enter height: ");
breadth = [Link]();
[Link]("Area of Triangle: " + (0.5 * length * breadth));
}
}