Java Inheritence / Chapter -4 / OOP's Concept


Java Inheritance

Inheritance



  1. It is also known as Is-A relationship.

  2. The main advantage of inheritance is code reuse-ability.

  3. By using extends keyword we can implement Is-A relationship.


example:-

class P

{

     public void m1()

    {

        System.out.println("Parent");

     }

}

class C extends P

{

   public void m2( )

   {

       System.out.println("Child");

    }

Class Test

{

     public static void main(String [ ] args)

    {

            C c =new C ( );

            c.m1( );

            c.m2( );

      }

}

 Output is:  



Parent

Child

            

Conclusion:-



  • Whatever method parent class has by default available to the child class and hence on the child reference we can call Both Parent & Child class methods.

  • Whatever method child has by default not available to the parent and hence on the parent reference we can't call child specific methods.

  • Parent reference can be used to hold child object but using that reference we can't call child specific methods but we can call the methods present in the parent class.

  • Parent reference can be used to hold child object but child reference cannot be used to hold parent object.


Multiple Inheritance



1.A java class can't extend more than one class at a time hence java won't provide support for multiple inheritance in classes.

Java Inheritance


2.If our class does not extend any other class then only our class is directly child class of object. 

 

Java Inheritance

 


3.If our class extends any other class then our class is the indirect child class of object class.  

 

Java Inheritance   

No comments:

Post a Comment

Be the first to comment!

Don't just read and walk away, Your Feedback Is Always Appreciated. I will try to reply to your queries as soon as time allows.

Note:
1. If your question is unrelated to this article, please use our Facebook Page.
2. Please always make use of your name in the comment box instead of anonymous so that i can respond to you through your name and don't make use of Names such as "Admin" or "ADMIN" if you want your Comment to be published.

Regards,
JavaByChetan
Back To Home

Flipkart End Of Season Sale






© Copyright 2017 Javabychetan.blogspot.com