Sunday, 27 August 2017

Inheritance in Java


What is Inheritance?

In real world every things (Objects) work in relationship Like Father-son ,Boss – Employee ,Teacher – Student.

To show this relationship in OOPS we used inheritance.

In inheritance there are two types of class; one is parent class and child class.

Child class can use property (Data members and functions) of parent class and add their own property(Data members and functions).

The concept of inheritance greatly improves the ability to reuse code as well as making design a much simpler and cleaner process

Child class inherit parent class using extends keyword.

Syntax
Class parent
{
      //veriable methods
}
Class child extends parents
{
      //can use parent variable
       //and methods
     // define own property
}



Two types of relationship

  • Is-A relationship
  • Has-A Relationship


Is-A relationship

"IS-A" relationship describes inheritance relationship between objects. If you can talk something with word "is". They can be described with keyword extends in coding. "is-a" relationship is also called classification.

  •  Mammal IS-A Animal
  •   Reptile IS-A Animal
  •  Dog IS-A Mammal

Hence : Dog IS-A Animal as well


Has-A relationship

HAS-A relationships are based on usage, rather than inheritance. In other words class Test HAS-A HasATest if class Test has a reference to an instance of class HasATest.

  • Horse HAS-A Animal. In other words,
  • Horse has a reference to a Animal.

There are 5 types of inheritance in java.

  • Single inheritance
  • Multilevel inheritance
  • Hierarchical inheritance
  • Multiple inheritance
  • Hybrid inheritance

(Note: Multiple & hybrid inheritance is not supported in java through class)


1) Single inheritance



When a class extends another one class only then we  call it a single inheritance. The below flow diagram shows that class B extends only one class which is A. Here A is a parent class of B and B would be  a child class of A.

Syntax
Class A
{
      //veriable methods
}
Class B extends A
{
      //can use A variable
       //and methods
     // define own property
}


Example
Class A
{
   public void methodA()
   {
     System.out.println("A class method");
   }
}

Class B extends A
{
   public void putValueB()
   {
     System.out.println("B class method");
   }
   public static void main(String args[])
   {
     B b = new B();
     b.putValueA(); //calling super class method
     b.putValueB(); //calling local method
  }
}

2) Multilevel Inheritance



In multilevel, one-to-one steps increases. Multiple classes are involved in inheritance, but one class extends only one. 

The lowermost subclass can make use of all its super classes’ members. Multilevel inheritance is an indirect method of implementing multiple inheritance. 

Following program explains.

Example
class crow
{
  public void nature()
  {
    System.out.println("Generally, crow fly");
  }
}
class Bird extends crow
{
  public void canEat()
  {
    System.out.println("Eats to live");
  }
}
public class Parrot extends Bird
{
  public void food()
  {
    System.out.println("Parrot eats fruits");
  }
  public static void main(String args[])
  {
    Parrot p = new Parrot();
    p.food(); // calling its own
    p.canEat(); // calling super class Bird method
    p.nature(); // calling super class Crow method
  }
}
Output:
Parrot eats fruits
Eats to live
Generally, crow fly

Now, Parrot has two super classes Bird and crow; but extended one-to-one. Parrot as a subclass can make use of the methods of Bird and crow. Bird can make use of the methods of crow only, but crow as a super class cannot access subclass members.
3     

      3) Hierarchical inheritance



In this inheritance multiple classes inherit from a single class i.e there is one super class and multiple sub classes

As we can see from the below figure when a same class is having more than one sub class (or) more than one sub class has the same parent is called as Hierarchical Inheritance.

Code
Class A
{
  public void methodA()
  {
     System.out.println("method of Class A");
  }
}
Class B extends A
{
  public void methodB()
  {
     System.out.println("method of Class B");
  }
}
Class C extends A
{
 public void methodC()
 {
 System.out.println("method of Class C");
 }
}
Class D extends A
{
  public void methodD()
  {
     System.out.println("method of Class D");
  }
}
Class MyClass
{
  public void methodB()
  {
     System.out.println("method of Class B");
  }
  public static void main(String args[])
  {
     B b1 = new B();
     C c2 = new C();
     D d3 = new D();
     b1.methodA();
     c2.methodA();
     d3.methodA();
  }
}
Output:

method of Class A
method of Class A
method of Class A




4) Multiple inheritance



“Multiple Inheritance” refers to the concept of one class extending (Or inherits) more than two or more base class.

The problem with “multiple inheritance” is that the derived class will have to handle the dependency on two base classes.

 Most of the new Object oriented programming languages like Small Talk, Java, C# do not support Multiple inheritance. Multiple Inheritance is supported in C++.

If you want to achieve multiple inheritance we can use concept of interface , using interface it is possible to achieve multiple inheritance.

5) Hybrid interface



As you can see in the above figure that it’s a combine form of single and multiple inheritance. 

Since java doesn’t support hybrid inheritance , the hybrid inheritance is also not possible.

If you want to achieve Hybrid interface we can use concept of interface,using interface it is possible to achieve Hybrid interface.




So in this tutorial we learned types if inheritance like single inheritance,multiple inheritance,multilevel inheritance,hierarchical inheritance,hybrid inheritance and has-A relationship and is-A relationship.

0 comments:

Post a Comment