What is Inheritance & its type in Java?-vishmy1.blogspot.com

Inheritance in Java

Inheritance:- Inheritance is a process that allows inheriting the method and properties of parent or superclass or deriving an object from an existing class.

It is a mechanism in which one object inherits all the properties and behaviors of a parent class object

Inheritance is a very important and widely applied feature of Object-Oriented Programming.

Super Class:- Inheritance means deriving the properties of one class into another class. The class whose properties are being derived is called as Base Class or Super Class or Parent Class.

Sub Class:- The class which is deriving the properties of Superclass is called Derived Class or Sub Class or Child Class.  

Why Inheritance?

In order to implement a tree-like structure to implement a parent-child relationship.

It makes possible the reusability of an already existing class.

It makes it possible for a child class to inherit all the properties of the parent class.

The class whose features are inherited is known as superclass or base class or parent class, the class that inherits the other class properties is known as a subclass or derived class or child class or extended class.

The subclass can have its own field and method in addition to the superclass field and method.



Syntax:-

class derived extends base class
{
     properties;
}

Note:- The extends is a keyword, that is used for inheritance.

Inheritance Example in Java:-
class A
{
     int x, y;
}
class B extends A // class A becomes base class and class B becomes derived class.
{
   int a, b;
   
  //  here, class b can use the data members x and y of base class A
}


Types of Inheritance in Java

There are five types of Inheritance:-

  1. Single Inheritance.
  2. Multilevel Inheritance.
  3. Hierarchical Inheritance.
  4. Hybrid Inheritance.
  5. Multiple Inheritance. 
What is Inheritance in Java?-vishmy1.blogspot.com
Types of Inheritance
Single Inheritance:- It is a single derived class created from a single base class

What is Inheritance in Java?-vishmy1.blogspot.com
Single Inheritance
Example of Single Inheritance:-  

class A  //base class
{
    int i, j;
    public void showbase()
    {
           System.out.println("i & J of base class are"+i+"&"+j);
    }
}
class B extends A  //derived class 
{
     int k;
     public void showderived()
    {
           System.out.println("k of derived class is"+k);
    }
    public void addall()
   {
         System.out.println("i+j+k=" +(i+j+k));
   }
}

class singleInheritance
{
    public static void main(String args[])
   {
          A  a1 = new A();
          B  b1 = new B();
          System.out.println("Using object of base class:");
          a1.i = 4;
          a1.j = 5;
          a1.showbase();
          System.out.println("Using object of derived class:");
          b1.i = 6;
          b1.j = 7;
          b1.k = 8;
          b1.showbase();
          b1.showderived();
          System.out.println("showing addition of all:");
          b1.addall();
          }
}

Multilevel Inheritance:-   A derived class will be inheriting a base class and as well as the derived class also acts as the base to the other class.  


What is Inheritance in Java?-vishmy1.blogspot.com
Multilevel inheritance

Note:- Here class C has no direct relation with class A, but class B behaves as an intermediate class and makes this inheritance possible. 

Example of Multilevel Inheritance:-

class A
{
    public void set()
    {
         System.out.println("this is base class A");
    }
}
class B extends A
{
     public void get()
     {
          System.out.println("this is derived class for class A & base class for class C");
     }
}
class C Extends B
{
     public void check()
     {
          System.out.println("this is derived class for class B");
     }
}
class multilevelInheritance
{
     public static void main(String args[])
     {
         A  a1 = new A();
         System.out.println("using object of class A:");
         a1.set();
         B b1 = new B();
        System.out.println("using object of class B:");
        b1.set();
        b1.get();
        C  c1 = new C();
        System.out.println("using object of class C:");
        c1.set();
        c1.get();
        c1.check();
    }
}
         
      

Hierarchical Inheritance:- When more than one derived class is created from a single base class then that inheritance is hierarchical Inheritance.   

What is Inheritance in Java?-vishmy1.blogspot.com
Hierarchical Inheritance

Example of Hierarchical Inheritance:-

class A
{
    public void set()
    {
         System.out.println("this is base class A");
    }
}
class B extends A
{
     public void get()
     {
          System.out.println("this is derived class for class A");
     }
}
class C Extends A
{
     public void check()
     {
          System.out.println("this is derived class for class A");
     }
}
class multilevelInheritance
{
     public static void main(String args[])
     {
         A  a1 = new A();
         System.out.println("using object of class A:");
         a1.set();
         B b1 = new B();
        System.out.println("using object of class B:");
        b1.set();
        b1.get();
        C  c1 = new C();
        System.out.println("using object of class C:");
        c1.set();
        c1.check();
    }
}

Hybrid Inheritance:-  Any combination of single, hierarchical, multiple and multilevel Inheritance is called Hybrid Inheritance, or we can say that Hybrid Inheritance is a combination of more than one type of Inheritance.

What is Inheritance in Java?-vishmy1.blogspot.com
Hybrid Inheritance

What is Inheritance in Java?-vishmy1.blogspot.com
                                                                    Hybrid Inheritance                                                                                                               

Example of Hybrid Inheritance:-

class A
{
    public void set()
    {
         System.out.println("this is base class A");
    }
}
class B extends A
{
     public void get()
     {
          System.out.println("this is derived class for class A");
     }
}
class C Extends B
{
     public void check()
     {
          System.out.println("this is derived class for class B");
     }
}

class D Extends B
{
     public void display()
     {
          System.out.println("this is derived class for class B");
     }
}

class multilevelInheritance
{
     public static void main(String args[])
     {
         A  a1 = new A();
         System.out.println("using object of class A:");
         a1.set();
         B b1 = new B();
        System.out.println("using object of class B:");
        b1.set();
        b1.get();
        C  c1 = new C();
        System.out.println("using object of class C:");
        c1.set();
        c1.get();
        c1.check();
        D d1 = new D();
       System.out.println("using object of class D:");
       d1.set();
       d1.get();
       d1.display(); 
   
 }

}

Multiple Inheritance:- When a derived class is created from more than one Base class, then that Inheritance is called as Multiple Inheritance. but  Multiple Inheritance is not supported by java. It can be done by using Interface. 


What is Inheritance in Java?-vishmy1.blogspot.com

                                                                 Multiple Inheritance                                

Example of  Multiple Inheritance:-

interface A
{
    public void show();// it's an abstract method without body.
}
interface B
{
    public void display();
 }
 class C implements A, B
{
    public void show()
    {
         System.out.println("Abstract method called:");
    }
    public void display()
    {
         System.out.println("it is interface B:);
    }
}
public static void main(String args[])
{
  C c1 = new C();
   c1.show();
   c1.display();
 }
}  

Note:- here, implements keyword is used for inheriting the properties of interface A & interface B.

Advantage and Disadvantage of  Inheritance in Java:-

Advantage:-

There are several advantages are in the Inheritance in Java:-

1)- One of the main advantage of the inheritance in java is it allows the derived class to inherit all the properties and behaviours of the base class.

2)- In Inheritance, we can override the methods of base class into the derived class so that meaningful implementation can be done in the derived class.

3)- Inheritance promotes the reusability when a derived class inherits all the functionalities and properties of the base class, reusability enhances the reliability of the code that is already tested and debugged in the base class.

Disadvantages:-

1)- The main disadvantage of the Inheritance in Java is that the two classes are tightly coupled such as base class and derived class, any change in the base class can affect the child classes, this means one class can't be used independently of each other.

2)- If we use Inheritance improper then it may lead to the wrong solution. the functions in the Inheritance work slower than the normal function as there is indirection.

3)- Less knowledge of Inheritance also may lead to wrong solutions, the data members of the parent class(base class) are left unused which may lead to memory wastage.


Note:- If you find anything incorrect, please write in comment.



In the next article, I am going to describe Interface in brief.



people also search for this:-

Garbage Collection in Java.
Static keyword in Java.
final keyword in Java.
this & super keywords in Java.








Post a Comment

3 Comments