Friday, October 25, 2019

What is Inheritance ? | Concept of inheritance in JAVA | JAVA Programming

What is Inheritance ? | Concept of inheritance in JAVA | JAVA Programming




What is Inheritance?


In this article we learn about inheritance in java with the help of simple examples.


Inheritance is the one of the part of or key features of object oriented programming (OOP) which allows us to define a new class from an existing class.

Example:

class Birds
{
          // eat() method
          // sleep() method
}
class Parrot extends Birds
{
          // fly() method
}


In Java Programming, we can use the extends keyword to inherit from a class.


Here, we have inherited the parrot class from the Birds class.


The Birds is the super or parent class and the parrot is a subclass or child class.


The subclass inherits the fields and methods of the super class.




is-a relationship


Inheritance is the one type of is-a relationship. We can use inheritance if and only if an is-a relationship is present between two classes.

Example:

A mango is a fruit.
A bus is a vehicle.
A parrot is a bird.
A cat is a animal.
A Neem is a tree.
A fan is an object.


Example of JAVA Inheritance:

class Birds
{
          public void eat()
          {
                    System.out.println( " I can eat " );
          }
          public void sleep()
          {
                    System.out.println( " I can sleep " );
          }
}

class Parrot extends Birds
{
           public void fly()
           {
                     System.out.println( " I can fly " );
           }
}

class main
{
          public static void main ( string[ ] args )
          {
                    Parrot.parrot1 = new Parrot();
                    
                    parrot1.eat();
                    parrot1.sleep();

                    parrot1.fly();
          }
}

Output:

I can eat
I can sleep
I can fly


In this example we have inherited a subclass Parrot from superclass Birds. 

The Parrot class inherits the methods eat() and sleep() from the Birds class.

So, object of the Parrot class can access the members of both the Parrot class and the Birds class.



Types of inheritance


There are five types of inheritance.

1. Single inheritance:

In this type of Inheritance Class B applies only to Class A.


2. Multi-level inheritance

In multi level inheritance class B extends class A;  then class C extends from class B.

 3. Hierarchical inheritance: 

In Hierarchical inheritance Class A acts as a superclass for classes B, C and D.


 4. Multiple inheritance:

In Multiple inheritance Class C extends from interfaces A and B.

 5. Hybrid inheritance

It is a combination of two or more types of inheritance.


Java programming should not support multiple and hybrid inheritance through classes. Also we can get multiple legacy in Java via interfaces.



Why use inheritance?

The most important use is the reuse of the code. The code in the parent class does not need to be rewritten in the child class.

To get run-time polymorphism by overloading a method.  We will learn more about polymorphism in future article.

Related Posts:

0 comments: