Java Inheritance - Java

What is Java Inheritance?

Inheritance may be defined as the procedure wherein one class acquires the properties (strategies and fields) of some other. With using inheritance the records is made manageable in a hierarchical order.

The class which inherits the properties of other is known as a subclass (derived class, child class) and the class whose properties are inherited is known as superclass (base class, parent class).

Extends keyword

Extends is the keyword used to inherit the properties of a class. Following is the syntax of extends keyword.

Syntax

Sample Code

Following is an example demonstrating Java inheritance. In this example, you can observe two classes namely Calculation and My_Calculation.

Using extends keyword, the My_Calculation inherits the methods addition() and Subtraction() of Calculation class.

Copy and paste the following program in a file with name My_Calculation.java

Example

Compile and execute the above code as shown below.

After executing the program, it will produce the following result −

Output

In the assumed program, when an object to My_Calculation class is formed, a copy of the contents of the superclass is made within it. That is why; using the object of the subclass you can access the members of a superclass.

inheritance

The Superclass reference variable can hold the subclass object, but using that variable you can access only the members of the superclass, so to access the members of both classes it is recommended to always create reference variable to the subclass.

If you consider the above program, you can instantiate the class as given below. But using the superclass reference variable ( cal in this case) you cannot call the method multiplication(), which belongs to the subclass My_Calculation.

Note − A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.

The super keyword

The super keyword is just like this key-word. Following are the scenarios in which the super keyword is used.

  • It is used to differentiate the members of superclass from the members of the subclass, in the event that they have equal names.
  • It is used to invoke the superclass constructor from the subclass.

Differentiating the members

If a class is inheriting the properties of any other class. And if the members of the superclass have the names same as the subclass, to distinguish these variables we use a super keyword as shown underneath.

Sample Code

This section provides you an application that demonstrates the usage of the super keyword.

Inside the given program, you've got classes namely Sub_class and Super_class, both have a method named display() with different implementations, and a variable named num with different values. we are invoking show() technique of each training and printing the value of the variable num of both classes. here you could look at that we've got used super keyword to differentiate the participants of superclass from the subclass.

Copy and paste the program in a file with call Sub_class.java.

Example

Compile and execute the above code using the following syntax.

On executing the program, you will get the following result −

Output

Invoking Superclass Constructor

If a class is inheriting the properties of another class, the subclass automatically acquires the default constructor of the superclass. But if you want to call a parameterized constructor of the superclass, you need to use the super keyword as shown below.

Sample Code

The program given on this phase demonstrates the way to use the super keyword to invoke the parameterized constructor of the superclass. This software incorporates a superclass and a subclass, in which the superclass carries a parameterized constructor which accepts a string value, and we used the super keyword to invoke the parameterized constructor of the superclass.

Copy and paste the subsequent program in a file with the call Subclass.java

Example

Compile and execute the above code using the following syntax.

On executing the program, you will get the following result −

Output

IS-A Relationship

IS-A is a way of saying: This object is a type of that object. Let us see how the extends keyword is used to achieve inheritance.

Now, based on the above example, in Object-Oriented terms, the following are true −

  • Animal is the superclass of Mammal class.
  • Animal is the superclass of Reptile class.
  • Mammal and Reptile are subclasses of Animal class.
  • Dog is the subclass of both Mammal and Animal classes.

Now, if we consider the IS-A relationship, we can say −

  • Mammal IS-A Animal
  • Reptile IS-A Animal
  • Dog IS-A Mammal
  • Hence: Dog IS-A Animal as well

With the use of the extends keyword, the subclasses will be able to inherit all the properties of the superclass except for the private properties of the superclass.

We can assure that Mammal is actually an Animal with the use of the instance operator.

Example

This will produce the following result −

Output

Since we have a good understanding of the extends keyword, let us look into how the implements keyword is used to get the IS-A relationship.

Generally, the implements keyword is used with classes to inherit the properties of an interface. Interfaces can never be extended by a class.

Example

The instanceof Keyword

Let us use the instanceof operator to check determine whether Mammal is actually an Animal, and dog is actually an Animal.

Example

This will produce the following result −

Output

HAS-A relationship

These relationships are mainly based on the usage. This determines whether a certain class HAS-A certain thing. This relationship helps to reduce duplication of code as well as bugs.

Lets look into an example −

Example


This indicates that class Van HAS-A speed. by having a separate class for speed, we do now not have to placed the complete code that belongs to speed in the Van class, which makes it possible to reuse the speed class in a couple of applications.

In object-oriented feature, the users do not need to hassle approximately which item is doing the actual work. To gain this, the Van class hides the implementation info from the customers of the Van class. So, essentially what happens is the users might ask the Van class to do a certain action and the Van class will either do the work by itself or ask another class to carry out the action.

Types of Inheritance

There are various types of inheritance as demonstrated below.

types_of_inheritance

A very important fact to remember is that Java does not support multiple inheritance. This means that a class cannot extend more than one class. Therefore following is illegal −

Example

However, a class can implement one or more interfaces, which has helped Java get rid of the impossibility of multiple inheritance.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Java Topics