0% found this document useful (0 votes)
1 views7 pages

Inheritance Program

The document demonstrates various types of inheritance in Java, including single, multilevel, hierarchical, and hybrid inheritance through classes like Animal, Dog, Puppy, Cat, and HybridChild. It also includes an abstract class Shape with subclasses Rectangle, Circle, and Triangle, allowing users to calculate the area of different shapes via a console interface. The main classes illustrate the usage of these inheritance structures and shape calculations interactively.

Uploaded by

vsumadeepthi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views7 pages

Inheritance Program

The document demonstrates various types of inheritance in Java, including single, multilevel, hierarchical, and hybrid inheritance through classes like Animal, Dog, Puppy, Cat, and HybridChild. It also includes an abstract class Shape with subclasses Rectangle, Circle, and Triangle, allowing users to calculate the area of different shapes via a console interface. The main classes illustrate the usage of these inheritance structures and shape calculations interactively.

Uploaded by

vsumadeepthi
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

// 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));
}
}

You might also like