Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples

October 26, 2015 Categories: Inheritance. 1 Comment on Java Inheritance Concepts

In this article we will look at what is inheritance? How inheritance is implemented in Java? How to work with inheritance in Java programs and various other Java inheritance related concepts.

 

Inheritance Definition

As already seen here: object orientation concepts, inheritance is one of the four corner stones of object oriented programming. Inheritance allows programmers to create an hierarchy of classes where while moving downwards (top to bottom) the hierarchy, specialization increases and while moving upwards (bottom to top) the hierarchy, generalization increases. Didn’t understand? First let’s look at the definition of inheritance.

Inheritance is defined as: Deriving properties and behavior of one class to another class.

 

Inheritance Basics

The class from which things are derived is known as super class and the class in to which things are derived to is known as sub class. A super class can also be called as base or parent class and a sub class can also be called as derived or child class.

Inheritance relationship between two classes is also known as is-a relationship or also called as generalization-specialization relationship. When one or more classes have general characteristics, such characteristics are moved to a general class (super class) and only special characteristics are maintained in the specialized class (sub class). Inheritance relationship can be represented as shown below:

java inheritance

In the above figure, class A is super class and class B is sub class. In Java programs, inheritance is implemented using extends keyword. For example, if a class B wants to derive the characteristics of class A, we can write as shown below:

Let’s look at how inheritance works using a more practical example given below:

In the above example, class Student is super class and class CSEStudent is sub class. You can see how general data and code is maintained in the super class and how specific data (branch and getBranch) is maintained in the sub class. Output of the above program is:

Student’s name is: Teja
Student’s registered number is: 10AB001
Student’s name is: 20
Student’s branch is: CSE

Don’t worry about the super keyword which is used inside the constructor of CSEStudent class. It will be covered in future article.

 

Advantages of Inheritance

Following are the advantages of using inheritance in programs:

  • Reusability: The code and methods declared in the base class can be re used in the derived class.
  • Extensibility: Derived classes can be extended to provide new functionality of their own.
  • Data Hiding: Base class can hide some of its data and code by making them private.
  • Overriding: Derived classes can have methods with same signature as in base class. The methods in the derived class provides suitable functionality which might be different from the methods available in the base class.

 

Disadvantages of Inheritance

Following are the disadvantages of using inheritance:

  • More time taken for the control to reach the base class from derived classes when there are several levels of inheritance.
  • Tight coupling between the base class and derived class.
  • Increase in maintenance time as changes done to base class may require changes to be performed in the derived class.

 

After looking at the definition of inheritance, basics of inheritance and advantages of inheritance, you can learn more about Java inheritance related concepts which are provided below:

Types of Inheritance: We will learn what are the different types of inheritance and which types are supported by Java. Read more on types of inheritance here.

super Keyword: The super keyword is only useful in the context of inheritance. Read more on super keyword here.

Constructors in Inheritance: How are constructors called in case of inheritance? Read more about constructors in inheritance here.

Method Overriding: When both super class and sub class contains same method, what happens? Read more on method overriding here.

Dynamic Method Dispatch: What is dynamic binding and when are methods bound dynamically? Learn more on dynamic method dispatch here.

abstract Keyword: The abstract keyword is used to implement abstraction is Java programs. Learn more about abstract keyword here.

final Keyword: The final keyword has special uses in the context of inheritance. Learn more about final keyword here.

Object Class: Object is the base class (super class) for all classes available in Java. Learn more about Object class here.

 

References

Related Links:

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

1 Comment

You can follow any responses to this entry through the RSS 2.0 feed.

good explanation for java keywords

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll Up
Thank you for visiting startertutorials.com! If you like this website do share with your friends by clicking on the social icons available on the left hand side.

As the domain name is quite large to remember, use this short domain to visit this site. Short domain: stuts.me