Monday, 11 June 2018

Inheritance

Inheritance in Java


In object-oriented programming, inheritance is the mechanism of basing an object or class upon another object  or class, retaining the same implementation. In most class-based object-oriented languages, an object created through inheritance (a "child object") acquires all the properties and behaviors of the parent object. Inheritance allows programmers to create classes that are built upon existing classes, to specify a new implementation while maintaining the same behaviors (realizing an interface), to reuse code and to independently extend original software via public classes and interfaces. The relationships of objects or classes through inheritance give rise to a directed graph.
An inherited class is called a subclass of its parent class or super class.

Types

  •     Single inheritance



where subclasses inherit the features of one superclass. A class acquires the properties of another class.


Example

      Base Class:-

      package hello;

      public class Parent {
  public void insert(String name, int age) {
   System.out.println("Name of Person :
                         "+name+"\n"+"Age : "+age);
  }
  public void add(int a, int b) {
int c = a+b;
System.out.println(c);
}

      }

      Derived Class:-


      package hello;

      public class Child extends Parent{
Child() {
}

public static void main(String[] args) {
Child c = new Child();
c.insert("Kapil", 25);
c.add(10, 12);

}

      }


      


  • Multilevel inheritance

where a subclass is inherited from another subclass. It is not uncommon that a class is derived from another derived class as shown in the figure "Multilevel inheritance".


Example

Super Base Class:-

      package hello;

   public class GrandParent {
public void country() {
System.out.println("Indian");
}

   }

  

 Base Class:-

      package hello;

    public class Parent extends GrandParent{
public void insert(String name, int age) {
super.country();
System.out.println("Name of Person :     "+name+"\n"+"Age : "+age);
}
}


 Derived Class:-

      package hello;

      public class Child extends Parent{
Child() {
}

public static void main(String[] args) {
Child c = new Child();
c.insert("Kapil", 25);

}

      }


Method Overriding

Many object-oriented programming languages permit a class or object to replace the implementation of an aspect—typically a behavior—that it has inherited. This process is usually called overriding.

Note:-
  1. method must have same name as in the parent class
  2. method must have same parameter as in the parent class.
  3. must be IS-A relationship (inheritance).


Example

      Base Class:-

      public class Engine{ 
           public void run(){
                      System.out.println("Engine is running");
                   }  
     }  

      Derived Class:-    

  public class Bike extends Engine{ 
     public static void main(String args[]){  
       Bike obj = new Bike(); 
       obj.run();  
     } 
  }

0 comments:

Post a Comment