Topics

6/recent/ticker-posts

Inheritance in java

Inheritance 


Generally the inheritance is a topic under oops concept where one class(base) is derived into several classes(derived). The Types of Inheritance are : 

        i) Single Inheritance

        ii) Multi-Level Inheritance

        iii) Multiple Inheritance

        iv) Hierarchial Inheritance

        v) Hybrid Inheritance


  • Single Inheritance

In Single Inheritance sub class/child class/derived class inherits "only one" parent class properties/variables and behaviors/methods thus enable reusability and as well adding new behaviors or modifying existing behaviors.


A Parent Class

                    ^

                             |

B Child Class

In inheritance child class must point to parent class, but not parent class to sub class. We must say Sub Class B is derived from Parent Class A.


  • Multi-Level Inheritance

In Multi-Level Inheritance each sub class derives from only one super class at a time, that super class again derives from only one more super class at a time.


A

^

        |

B

^

|

C

  • Multiple Inheritance

        In Multiple Inheritance a sub class can derive from any number of super classes, such super classes each can again derive from any number of super classes.


A1 A2 A3  B1 B2   C1 C2 C3

|       |      |     |    |         |      |     |

    x              x                   x

------------------------------------

A B C

x y x

| | |

------------------------- 

^

|

D

x

print(this.x)

print(super.x)

D d=new D();


  • Hierarchial Inheritance

In Hierarchial Inheritance a class can be derived into any number of sub classes.

A

|

---------------------------

| | |

B C D

------------------------------------------

 |      |    |           |    |         |      |     |     |

B1 B2 B3 C1 C2 D1 D2 D3 D4


  • Hybrid Inheritance

Hybrid Inheritance is a combination of hierarchical and multiple inheritance.

A

|

-----------------------

| |

B C

-----------------------

|

D

Imp Point: Java supports only multi-level inheritance. Hence in Java each class can derive from only one super class at a time.


class A {

void m1() {

System.out.println("m1() of A called ");

}

}


class B extends A {

void m2() {

System.out.println("m2() of B called ");

}

}


class C extends B {

void m3() {

System.out.println("m3() of B called ");

}

}


class D {


public static void main(String rags[]) {

A a=new A();

a.m1();

B b=new B();

b.m1();

b.m2();

C c=new C();

c.m1();

c.m2();

c.m3();

// super class object reference assigned to sub class reference

// B b=new A(); // gives error

// sub class object assigned to super class reference

A ab=new B();

ab.m1();

// ab.m2(); // cannot be called, only B and A class common methods can only be called

A ac=new C();

ac.m1();

// ac.m2(); // error

// ac.m3(); // error

B bc=new C();

bc.m1();

bc.m2();

// bc.m3(); // error

}

}


Examples on assigning sub class objects to super class references. Remember only common functions can be called.


Frame f =new MyFrame();

Panel p=new MyPanel();

Emp e=new PermanentEmp();

Student s=new DayScholarStudent();

Customer c=new GoldCardCustomer();




Post a Comment

0 Comments