Friday, January 6, 2017

JAVA OOP Inheritance

What is Inheritance in JAVA


Case 1

In this Example you can see how we can inherit one classes property to (attributes and Methods) another one.Consider this example below and you'll see how we inherit A's property to by using
keyword “extends”.

Without that keyword we are unable to inherit that properties.


class A{

   int a;
   static{
         System.out.println("static of A");
   }


    void printA(){
         System.out.println("A : "+a);
    }
}


class B extends A{ //<-- extends
   int b;
   static{
       System.out.println("static of B");
   }


    void printB(){
       System.out.println("B : "+b);
    }

    void printAB(){
       System.out.println("A : "+a);
       System.out.println("B : "+b);
    }

    void callPrintAB(){
       printA();
       printB();
   }

}

class Demo{
     public static void main(String args[]){
      B b1=new B();
     }
}


Try your self and the output will be

static of A
static of B

Because the static blocks executes before creating the objects.
For a standard def we can say inheritance is , Make a new class using defined existing
class for any purpose. And it is Re Usable.

This is similar to creating a Calculator using Jframe class.whether it is defined by James Gosling
we can re use it for our newer purposes.

Ok guys this is the rough idea of Inheritance hope you will understand and this is enough to understanding purpose so I will upload some problems to “COMPETE ” page.Please do join with that...Please join with my next post which is the next case of inheritance...
Have a nice day !


0 comments:

Post a Comment