oop1
oop1
Advantages of Inheritance
Minimizing duplicate code: Key benefits of Inheritance include minimizing the identical code as it
allows sharing of the common code among other subclasses.
Flexibility: Inheritance makes the code flexible to change, as you will adjust only in one place, and
the rest of the code will work smoothly.
Overriding: With the help of Inheritance, you can override the methods of the base class.
Data Hiding: The base class in Inheritance decides which data to be kept private, such that the
derived class will not be able to alter it.
Types of Inheritance
There are different forms of inheritance in java
The extends keyword indicates that making a new class that derives from an existing class.
The subclass will now contain its own variables and methods as well those of the superclass.
This kind of situation occurs when we want to add some more properties to an existing class
without actually modifying it.
Example,
class A extends B {
// Here Class A will inherit the features of Class B
}
Syntax,
class A
{
------
Multilevel Inheritance
Multilevel inheritance is derived from a derived class.
Multilevel inheritance allows building a chain of classes as shown in figure.
We can include any number of levels in multilevel inheritance.
For example, Class A is a superclass for the Class Band Class B is a superclass for the subclass,
Class C.
Example,
class Animal
{
void eat()
{
System.out.println("eating...");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}
}
class BabyDog extends Dog
{
void weep()
{
System.out.println("weeping...");
Output:
weeping
barking
eating
In the example given above ,BabyDog class inherits the Dog class which again inherits the Animal class,
so there is a multilevel inheritance.
Hierarchical Inheritance
In Hierarchical inheritance one parent class will be inherited by many sub classes.
Some features of one level are shared by many others below the level.
One super class and many sub classes
As per the above example Class A will be inherited by Class B, Class C and Class D.
Class A will be acting as a parent class for Class B, Class C and Class D.
Syntax,
ClassA
{
-------
}
Class B extends A
{
-------
Example,
In the example given below, Dog and Cat classes inherits the Animal class, so there is Hierarchical
inheritance.
class Animal
{
void eat()
{
System.out.println("eating...");
}
}
class Dog extends Animal
{
void bark()
{
System.out.println("barking...");
}
}
class Cat extends Animal
{
void meow()
{
System.out.println("meowing...");
}
}
class TestInheritance3
{
public static void main(String args[])
{
Cat c=new Cat();
c.meow();
c.eat();
//c.bark();//gives error
}
}
Hybrid Inheritance
It is a mix of two or more of the above types of inheritance.
Since Java doesn’t support multiple inheritances with classes, hybrid inheritance involving multiple
inheritance is also not possible with classes.
In Java, we can achieve hybrid inheritance only through Interfaces if we want to involve multiple
inheritance to implement Hybrid inheritance.
It can be achieved through a combination of Multilevel Inheritance and Hierarchical Inheritance
with classes, Hierarchical and Single Inheritance with classes.