Java Inheritance
One of the fundamental concepts of OOP is inheritance, which enables us to derive a new class from an existing one.
The existing class from which the child class is derived is known as the superclass
, and the newly produced class is known as the subclass
(child or derived class) (parent or base class).
In Java, inheritance is accomplished through the extends keyword.
Syntax for inheritance
class Childclass extends Parentclass
{
//methods and fields
}
Here, using the extends
keyword, we inherit all the property, methods of Parentclass
in the Childclass
. Now, the Childclass
can access all the properties and methods of the Parentclass
.
There are different types of inheritance in java. They are,
- Single Inheritance
- Multilevel Inheritance
- Hierarchical Inheritance
- Multiple Inheritance
- Hybrid Inheritance
Single Inheritance
Single inheritance occurs when one class inherits from another.
Here, the ClassB
is inherited by ClassA
, so ClassA
is sub-class and ClassB
is the super-class.
Example for single-inheritance
class Animal{
void Eat()
{
System.out.println("Eat");
}
}
class Dog extends Animal{
void Bark()
{
System.out.println("Bark");
}
}
class Main{
public static void main(String args[]){
Dog dog1=new Dog();
dog1.Bark(); // Bark
dog1.Eat(); // Eat
}
}
Output returned by executing above program
Bark
Eat
Here, Animal
class is the superclass with a function Eat()
and this function is inherited by the Dog
class. The Dog
class has a function Bark()
.
Now when creating an object for Dog
class we can access the Eat()
of Animal class and its own Bark()
function.
Multi-level Inheritance
Multi-level inheritance is the concept of inheriting a class and the inherited class will be further inherited by other class. So it acts like a chain of inheritance.
Here, ClassC
is inherited by ClassB
and then ClassB
is inherited by ClassA
. This type of inheritance is known as multi-level inheritence.
Example for multi-level inheritance
class Animal{
void Eat()
{
System.out.println("Eat...");
}
}
class Dog extends Animal{
void Bark()
{
System.out.println("Bark...");
}
}
class Puppy extends Dog{
void Play()
{
System.out.println("Play...");
}
}
class Main{
public static void main(String args[]){
Puppy puppy=new Puppy();
puppy.Bark();
puppy.Eat();
puppy.Play();
}
}
On running the above program, it will generate the result below.
Bark...
Eat...
Play...
Here, Animal
-> Dog
-> Puppy
is the inheritance graph. So the class Puppy
will have all the properties of Animal
and the Dog
class along with its own properties and function.
Hierarchical Inheritance
Hierarchical inheritance is the process in which two or more classes inherit properties from a single class. Here the ClassA
is inherited by ClassB
and ClassC
.
Example for Hierarchical Inheritance
class Animal{
void Eat()
{
System.out.println("Eat...");
}
}
class Dog extends Animal{
void Bark()
{
System.out.println("Bark...");
}
}
class Cat extends Animal{
void Meow()
{
System.out.println("Meow...");
}
}
class Main{
public static void main(String args[]){
Cat cat=new Cat();
cat.Eat();
cat.Meow();
}
}
Output
Eat...
Meow...
In the above example, the Animal
class is inherited by Dog
and Cat
class. So with the object of the Cat
class we can access the Eat()
function of Animal
class.
Multiple Inheritance
In multiple inheritance, a single class with try to inherit multiple classes.
This type of inheritance is not supported in java. We use interface to achieve this type of inheritance.
Multiple inheritance is not enabled in Java in order to simplify the language and reduce its complexity.
Hybrid Inheritance
Combining two or more different inheritance types is known as hybrid inheritance.
This type of inheritance is also not supported in java.