Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: Inheritance. 1 Comment on Java Inheritance Concepts
5
(2)

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.

 


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


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:

class B extends class A
{
	//Members of class B
}

 

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

class Student
{
	String name;
	String regdNo;
	int age;
	Student(String name, String regdNo, int age)
	{
		this.name = name;
		this.regdNo = regdNo;
		this.age = age;
	}
	public void getName()
	{
		System.out.println("Student's name is: "+name);
	}
	public void getRegdNo()
	{
		System.out.println("Student's registered number is: "+regdNo);
	}
	public void getAge()
	{
		System.out.println("Student's name is: "+age);
	}
}
class CSEStudent extends Student
{
	static final String branch = "CSE";
	CSEStudent(String name, String regdNo, int age)
	{
		super(name, regdNo, age);
	}
	public void getBranch()
	{
		System.out.println("Student's branch is: "+branch);
	}
}
public class Driver 
{
	public static void main(String[] args) 
	{
		CSEStudent s1 = new CSEStudent("Teja", "10AB001", 20);
		s1.getName();
		s1.getRegdNo();
		s1.getAge();
		s1.getBranch();
	}
}

 

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

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

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 *