inheritance


Inheritance
ü  It is a process of acquiring one class properties into other class that is accessing one class members in another class without creating object for that class.
ü  The process is done by using a keyword known as “extends”.
ü  With the sub class object we can access sub class as well as superclass members.
ü  And with the superclass object we can access only super-class members.
ü  If it is not inheriting anything automatically it extends java.lang.Object
Types of Inheritance
1. Single level inheritance: ---
In this one class is extended from another class i.e. one parent have one child . 
              Parent
                  |                  
              Child

Single inheritance
class parent  {
 parent() {
   System.out.println("parent constructor");
}
static int d=8;
int x=9;
void m2()  {
    System.out.println("m2 from parent");
}
}
class child extends parent  {
 child() {
   System.out.println("child constructor");
}
void m1()  {
    System.out.println("m1 from parent");
}
public static void main(String [] args) {
child c=new child();
c.m1();
c.m2();
System.out.println(c.x);
System.out.println(d);
}
}
//note:--When we are creating object for sub-class internally the super class
              Constructor is invoked from our sub-class constructor with super ().
2.    Multilevel Inheritance :-                                                                   
                                       


Ex1:--
class B {
   B(){
    System.out.println("B cons");
    }
void area(){
int l=9,b=4;
 System.out.println("area of the rect is:" + (l*b));
}
}
class D1 extends B  {
     D1(){
      System.out.println("D cons");
    }
void perimeter(){
 int l=34,b=56;
System.out.println("The rect perimeter is:" + ((2*(l+b))));
    }
public static void main(String [] args){
D1 dd=new D1();
dd.perimeter();
dd.area();
}
}
class D2 extends D1{
D2(){
System.out.println("d2 cons");
    }
void vol(){
int x=3,y=4,z=5;
System.out.println("vol is:" +(x*y*z));
    }
public static void main(String [] args){
D2 obj=new D2();
obj.perimeter();
obj.area();
obj.vol();
}
}
3 . Multiple Inheritance : -(Not Supported by java using classes)


Note: ---In multiple inheritance if any two or more classes having same method prototype ,if we are calling that particular method using sub-class object, then compiler goes to confusion situation, known as ambiguity, that’s why JAVA doesn’t support this. This problem is resolved with Interface concept.
4 . Hybrid inheritance : -


class B {
void m1(){
System.out.println("m1 from base");
}  }
class D1 extends B {
void m2(){
System.out.println("m2 from D1");
}  }
class D2 extends B  {
void m3() {
System.out.println("m3 from D2");
}  }
class D4 extends D2  {
void m4(){
System.out.println("m4 from D4");
} }
class D5 extends D4 {
void m5(){
System.out.println("m5 from D5");
}
public static void main(String [] args){
System.out.println("class d5 started");
D5 b=new D5();
b.m1();   b.m3();   b.m4();  b.m5();
//b.m1();
System.out.println("class D5 ended");
}
}
class d3 extends B  {
void m6() {
 System.out.println("m6 from D3");
}
 }

5 . Hierarchical Inheritance : -






No comments:

Post a Comment