written 2.6 years ago by |
Java Inheritance
Inheritance is one of the main features of OOP that allows us to create a new class from an existing class.
Due to inheritance, it is possible to inherit attributes and methods from one class to another.
Inheritance can create two classes as follows:
- Subclass - The new class that is created is known as a subclass or child or derived class.
- Superclass - The existing class from which the child class is derived is known as the superclass parent or base class.
The "extends" keyword is used to perform inheritance in Java.
In the given program, Dog2, Cat2, Fish 2, and Bird2 are the subclasses or child classes that inherit the attributes name and age from the parent or superclass Animal.
Java Polymorphism
Polymorphism is one of the main features of OOP which says more than one form.
Polymorphism occurs when we have many classes that are related to each other by inheritance.
That means the same method or operator or object can perform different operations in different scenarios.
In the given program, a superclass called Animal has a method called show().
Subclasses of Animals could be Dog2, Cat2, Fish 2, and Bird2 also have their implementation of method show().
In short, Inheritance lets us inherit attributes and methods from another class.
Polymorphism uses those methods to perform different tasks.
This allows us to perform a single action in different ways.
Java Program
class Animal // Parent or Superclass Animal
{
String name; // Animal attribute name
int age; // Animal attribute age
public void show() // Animal method show
{
System.out.println("\nThese all are the animals of PetOwner2");
}
}
class Dog2 extends Animal // Child or Subclass Dog2
{
public void show() // Child class Dog2 methos show
{
System.out.println("\nThe Dog name is " + name + " and age of the Dog is " + age);
}
}
class Cat2 extends Animal // Child or Subclass Cat2
{
public void show() // Child class Cat2 method show
{
System.out.println("\nThe Cat name is " + name + " and age of the Cat is " + age);
}
}
class Fish2 extends Animal // Child or Subclass Fish2
{
public void show() // Child class Fish2 method show
{
System.out.println("\nThe Fish name is " + name + " and age of the Fish is " + + age);
}
}
class Bird2 extends Animal // Child or Subclass Bird2
{
public void show() // Child class Bird2 method show
{
System.out.println("\nThe Bird name is " + name + " and age of the Bird is " + age);
}
}
class PetOwner2 // The main class PetOwner2 for testing
{
public static void main(String[] args)
{
Animal a = new Animal (); // Create a parent class Animal object
Animal d = new Dog2(); // Create a child class Dog2 object
Animal c = new Cat2(); // Create a child class Cat2 object
Animal f = new Fish2(); // Create a child class Fish2 object
Animal b = new Bird2(); // Create a child class Bird2 object
// Access attributes of parent class Animal
d.name = "Dog2";
d.age = 10;
c.name = "Cat2";
c.age = 8;
f.name = "Fish2";
f.age = 2;
b.name = "Bird2";
b.age = 14;
d.show(); // Call the show method form Dog2 subclass using Dog2 object
c.show(); // Call the show method form Cat2 subclass using Cat2 object
f.show(); // Call the show method form Fish2 subclass using Fish2 object
b.show(); // Call the show method form Bird2 subclass using Bird2 object
a.show(); // Call the show method form Animal superclass using Animal object
}
}
The output of the Java Program -