Friday, 10 August 2018

Need and Benefits of Inheritance in Java

Inheritance in java is a mechanism in which one object acquire all the properties and behavior of parent class. Inheritance is an important OOP concepts where it provides code reusability and  run time polymorephism.

 The idea behind inheritance in java is that you can create new class from the already existing class. when you inherit from an existing class, you can reuse methods and fields of parent class, and you can add new methods and fields also.

 Why Inheritance in java?

                There are two main reasons why java soft peoples introduced inheritance in java, the first one is to achieve code reusability and to achieve run time polymorphism. Inheritance is a good choice when:
  • Your inheritance hierarchy represents an "is-a" relationship and not a "has-a" relationship.
  • You can reuse code from the base classes.
  • You need to apply the same class and methods to different data types.
  • The class hierarchy is reasonably shallow, and other developers are not likely to add many more levels.
  • You want to make global changes to derived classes by changing a base class.

How to Inherit a class ?

 To inherit a class features into another class you need to extend it using " extends" keyword like 

 class subClassName extends superClassName

       variable declarations;
       method definition;

For example, If you wish to inherit class "A" features in to class "B" then use as follows

 class B extends A

  // your sub class code here


  1. Super class: The class whose features are inherited is class super class / parent class / base class. In the above example class A is super class.
  2. Sub class: The class that inherits the other class is class sub class / child class / derived class.
  3. When you inherit one class features into another i.e, into sub class it contains all the features of super class and sub class unique features.
  4. In inheritance process creating object to derived class and calling super class methods is fashion but it not rule.



Post a Comment

Note: only a member of this blog may post a comment.

Find Us On Facebook

python tutorial


C Programming


Java Tutorial


Data Structures


MS Office


Database Management