Sunday, May 10, 2009

explain different types of inheritance with suitable examples

http://www.roseindia.net/java/language/inheritance.shtml

1 comment:

  1. Inheritance

    As the name suggests, inheritance means to take something that is already made. It is one of the most important feature of Object Oriented Programming. It is the concept that is used for reusability purpose. Inheritance is the mechanism through which we can derived classes from other classes. The derived class is called as child class or the subclass or we can say the extended class and the class from which we are deriving the subclass is called the base class or the parent class. To derive a class in java the keyword extends is used. To clearly understand the concept of inheritance you must go through the following example.

    The concept of inheritance is used to make the things from general to more specific e.g. When we hear the word vehicle then we got an image in our mind that it moves from one place to another place it is used for traveling or carrying goods but the word vehicle does not specify whether it is two or three or four wheeler because it is a general word. But the word car makes a more specific image in mind than vehicle, that the car has four wheels . It concludes from the example that car is a specific word and vehicle is the general word. If we think technically to this example then vehicle is the super class (or base class or parent class) and car is the subclass or child class because every car has the features of it's parent (in this case vehicle) class.

    The following kinds of inheritance are there in java.

    Simple Inheritance
    Multilevel Inheritance

    When a subclass is derived simply from it's parent class then this mechanism is known as simple inheritance. In case of simple inheritance there is only a sub class and it's parent class. It is also called single inheritance or one level inheritance.

    eg.

    class A {
    int x;
    int y;
    int get(int p, int q){
    x=p; y=q; return(0);
    }
    void Show(){
    System.out.println(x);
    }
    }

    class B extends A{
    public static void main(String args[]){
    A a = new A();
    a.get(5,6);
    a.Show();
    }
    void display(){
    System.out.println("B");
    }
    }
    Multilevel Inheritance

    It is the enhancement of the concept of inheritance. When a subclass is derived from a derived class then this mechanism is known as the multilevel inheritance. The derived class is called the subclass or child class for it's parent class and this parent class works as the child class for it's just above ( parent ) class. Multilevel inheritance can go up to any number of level.
    e.g.

    class A {
    int x;
    int y;
    int get(int p, int q){
    x=p; y=q; return(0);
    }
    void Show(){
    System.out.println(x);
    }
    }
    class B extends A{
    void Showb(){
    System.out.println("B");
    }
    }

    class C extends B{
    void display(){
    System.out.println("C");
    }
    public static void main(String args[]){
    A a = new A();
    a.get(5,6);
    a.Show();
    }
    }
    Java does not support multiple Inheritance

    Multiple Inheritance

    The mechanism of inheriting the features of more than one base class into a single class is known as multiple inheritance. Java does not support multiple inheritance but the multiple inheritance can be achieved by using the interface.

    In Java Multiple Inheritance can be achieved through use of Interfaces by implementing more than one interfaces in a class.

    ReplyDelete