Friday, 5 September 2014

Inheritance in Java



DEFINITION OF INHERITANCE :

Inheritance is one of the cornerstones of Object Oriented Programming (OOP) which means to inherit the properties of one class and extends according to the need. The two keywords used in inheritance are extends (for class) and implements (for interface).

Inheritance allows the creation of hierarchical classifications. In inheritance a general class is used to define the common methods and variables that can be used in other class. The class which inherits the general class will get all the public methods and variables present in the general class. Now, general class is called super class or base class and the class which inherits its properties is knows as sub class or child class.

To understand the concept of inheritance, consider an example :

Suppose you want to calculate the area of a square, a rectangle, a circle having same radius as length of square than by using the same length available in the base class Shape you can easily extends that class to Square class or rectangle class or circle class by the use of extends keyword.

 class Shape  
 {  
      //define a length which is public and can be used in square,   
      //rectangle and circle classes  
      public int length = 10;  
 }  
   
 //inherit the properties of Shape class with the use of extends keyword  
   
 class Square extends Shape  
 {  
      void area()  
      {  
           //calculate area of square  
           int area = length * length;  
   
           //print it on the screen  
           System.out.println("Area of square : "+area);  
      }  
 }  
   
 class Rectangle extends Shape  
 {  
      void area()  
      {  
           //define a breadth  
           int breadth = 7;  
           //calculate area of rectangle  
           int area = length * breadth;  
           //print on the screen  
           System.out.println("Area of rectangle : "+area);  
      }  
 }  
   
 class Circle extends Shape  
 {  
      void area()  
      {  
           //calculate area of circle using length of the shape class as radius  
           float area = 3.14f * length * length;  
           //print on the screen  
           System.out.println("Area of circle : "+area);  
      }  
 }  
   
 //make a entry class which contains main method  
 class InheritanceDemo  
 {  
      public static void main(String[] args)  
      {  
           //object of child class square  
           Square sq = new Square();  
           //object of child class rectangle  
           Rectangle rec = new Rectangle();  
           //object of child class circle  
           Circle cir = new Circle();  
             
           //call the area methods of all child class to   
           //get the area of different objects  
           sq.area();  
           rec.area();  
           cir.area();  
      }  
 }  

Output :




Why we use Inheritance?

Inheritance is used because it provides the Reusability of the code like in the above example we used the same length from the Shape class thrice in different three classes.

Types of Inheritance in Java :

There are three general types of inheritance in java. These are :

1.  Simple Inheritance : In simple inheritance there is one base class and a single child class which extends the properties of base class.

     2. Multilevel Inheritance : In multilevel inheritance there is single parent and a child class and a further sub child class which inherit properties of child class.

Hierarchical Inheritance : In this type of inheritance many child classes inherit the properties of the same child class like the example used in this blog post.


Note : Multiple Inheritance in which a class inherits properties of more than one class is not present in Java to remove the ambiguity in the code.

20 comments :

  1. System Programming
    Thanks for your ideas. You can also find the details on Affity Solutions, at the System Programming. The main object of the Affity Solutions is to provide quality web services and best training of software languages

    ReplyDelete
  2. thank for you useful infrormation and excellent blog in java
    best online trainings in java

    ReplyDelete
  3. Good material and very helpful for everyone who’s want to self learn. Types of Inheritance in Java

    ReplyDelete
  4. This comment has been removed by the author.

    ReplyDelete
  5. Well researched concepts..Thanks for sharing..java training in chennai

    ReplyDelete
  6. It is really a great work and the way in which u r sharing the knowledge is excellent.Thanks for helping me to understand basic concepts. As a beginner in java programming your post help me a lot.Thanks for your informative article.java training in chennai |

    ReplyDelete
  7. Inheritance is a part of the oops concept and it is a very important factor in programming. Thanks a lot for sharing such nice information. Keep up the good work going on.

    ReplyDelete
  8. Inheritance is used because it provides the Reusability of the code like in the above example we used the same length from the Shape class thrice in different three classes.perfect explanation about inheritance .its very useful.thanks for your valuable information.java training in chennai | top 10 java training institutes in chennai

    ReplyDelete
  9. I was doing an online research on How to Write a Personal Experience Essay Introduction before I landed on this amazing post and I have learned a lot from the shared article. I wonder where I could have found such information if it was not posted on this site therefore thanks the author for sharing the article with us. I was doing an online research on How to Write a Personal Experience Essay Introduction before I landed on this amazing post and I have learned a lot from the shared article. I wonder where I could have found such information if it was not posted on this site therefore thanks the author for sharing the article with us.

    ReplyDelete
  10. Thank you for your post. This is excellent information. It is amazing and wonderful to visit your site.
    student internships in banjarahills
    internship programs in madhapur

    ReplyDelete
  11. It is very good and very informative. There is a useful information in it.Thanks for posting..
    https://www.apponix.com/Java-Institute/Java-Training-Institute-in-Bangalore.html

    ReplyDelete
  12. I need to to thank you for your time due to this fantastic read!! I definitely enjoyed every bit of it and I have you bookmarked to see new information on your blog.
    Java Training in Bangalore

    ReplyDelete
  13. Hello, i think that i saw you visited my weblog thus i came to “return the favor”.I'm trying to find things to improve my site!I suppose its ok to use a few of your ideas!! mac neukölln berlin

    ReplyDelete
  14. This a informative blog, Inheritance is a concept of java programming language. Thanks for sharing this topic with us. If anyone interested to learn more about java, they can join an institute for java course. There are many java course service providers which provide best java course in Kanpur, Mumbai, Delhi, Pune, and other cities in India.

    ReplyDelete
  15. Your blog content is so knowledgeable and easily understandable. Keep on posting blog like this. If anyone interested to make career on Pyhton, There are many IT courses service providers which provide best Python Course with Job Placement in Jaipur, Kanpur, Mumbai, Delhi, Pune, and other cities in India.

    ReplyDelete