inheritance in java

Inheritance in Java-: Inheritance is the feature of object oriented programming system. Inheritance is the process by which object of one class acquire the properties of another class. It support the concept of hierarchical classification. For example the bird ‘robin’ is a part of the class ‘flying bird’ which is again part of  class ‘bird’. The principle behind the sort of division is that each derived class shares common characteristics with the class from which it is derived  as like

inheritance in java
java inheritance

Define Derived class in java-: A derived can be defined by specifying its relationship with the base class in addition to its own details. The general form of defining a derived class is like here..

class derived-class-name : visibility-mode base-class-name
  {
   .....//
   .....// member of derived class
   .....//
  }

The colon indicates that the derived class name is derived from the base class name. The visibility made is optional and if present.May be either private or public. The default visibility mode is private. Visibility mode specifies whether the features of the base class are privately derived or publicly derived . like here

class netnic: private xyz
 {
   member of abc
 }

class netnic: public xyz
 {
   member of abc
 }

 

TYPES OF INHERITANCE IN JAVA

 

Java Inheritance are divided according the class. there are

  1. Single inheritance
  2. Hierarchical inheritance
  3. Multilevel inheritance
  4. Hybrid inheritance

 

Single Inheritance-:  Single inheritance are simple inheritance in java. In this there are two class one is the base class and another is derived class in java. for example

class animal{  
           void eat(){System.out.println("eating the food");}  
          }  
class Lion extends Animal{  

             void bark(){System.out.println("roaring ");}  
               
      }  
class TestInheritance{  
      
     public static void main(String args[]){  
     Lion d=new Lion();  
     d.roar();  
    d.eat();  
}} 

Output of this programme

eating the food
roaring

 

Hierarchical inheritance-: Another interesting application of inheritance is to use it as a support to the hierarchical design of a programme.  Many programming problem can be cast into a hierarchy  where certain features on one level are shared by many others below level . For example students in a university. A student read Arts or Engineering  or  Medical  and in engineering department student read M.Tech or Civil or Electrical means in hierarchical inheritance have a sub level. for example here are four class are its name is netnicA,netnicB,netnicC,netnicD are the name of  class

 

public class netnicA 
{
    public void dispA()
    {
        System.out.println("disp() method of netnicA");
    }
}
public class netnicB extends netnicA 
{
    public void dispB()
    {
        System.out.println("disp() method of netnicB");
    }
}
public class netnicC extends netnicA
{
    public void dispC()
    {
        System.out.println("disp() method of netnicC");
    }
}
public class netnicD extends netnicA
{
    public void dispD()
    {
        System.out.println("disp() method of netnicD");
    }
}
public class HierarchicalInheritanceTest 
{
    public static void main(String args[])
    {
        //Assigning netnicB object to netnicB reference
        netnicB b = new netnicB();
        //call dispB() method of netnicB
        b.dispB();
        //call dispA() method of netnicA
        b.dispA();
        
        
        //Assigning netnicC object to netnicC reference
        netnicC c = new netnicC();
        //call dispC() method of netnicC
        c.dispC();
        //call dispA() method of netnicA
        c.dispA();
        
        //Assigning netnicD object to netnicD reference
        netnicD d = new netnicD();
        //call dispD() method of netnicD
        d.dispD();
        //call dispA() method of netnicA
        d.dispA();
    }
}

Output of this programme

disp() method of netnicB
disp() method of netnicA
disp() method of netnicC
disp() method of netnicA
disp() method of netnicD
disp() method of netnicA

 

Multilevel Inheritance-: It is not uncommon that a class is derived from another derived class. For a example The class A serves as a base class for the derived class B which in turn serves as a base class for the derived class C. The class B is known as intermediate base class since it provide a link for the inheritance between A and C . The chain  ABC is known as inheritance path.

Example of multilevel inheritance in java here are three class are its name is netnicA,netnicB,netnicC, is name of the class

 

public class netnicA 
{
    public void dispA()
    {
        System.out.println("disp() method of netnicA");
    }
}
public class netnicB extends netnicA 
{
    public void dispB()
    {
        System.out.println("disp() method of netnicB");
    }
}
public class netnicC extends netnicB
{
    public void dispC()
    {
        System.out.println("disp() method of netnicC");
    }
    public static void main(String args[])
    {
        //Assigning netnicC object to netnicC reference
        netnicC c = new netnicC();
        //call dispA() method of netnicA
        c.dispA();
        //call dispB() method of netnicB
        c.dispB();
        //call dispC() method of netnicC
        c.dispC();
    }
}

Output of this programme

disp() method of netnicA
disp() method of netnicB
disp() method of netnicC

 

Hybrid Inheritance -:  It is the combination of single and multiple inheritance. For example consider the case of students result. In this we have give weightage  for sports before final list the result. The weightage for sports is stored in a separate class called Sports . The new inheritance relationship between the various classes called hybrid inheritance. for example

class sports
{
 protected
   float score
 public:
    void get_score(float)
    void put_score(void)
}

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *