Object Inheritance (polymorphic classes) In Java

Object Inheritance (polymorphic classes) In Java

In Java, inheritance can be defined as the mechanism in which an object acquires all the behaviors and properties of the parent object. Object inheritance is a vital concept in object-oriented programming. The idea of inheritance is used in Java to create new classes that are built upon existing classes. Inheriting from an existing class means you reuse fields and methods of the parent class. Additionally, you can also add new fields and methods in your current class.

Inheritance refers to the parent-child relationship. It represents the IS-A relationship.

Why inheritance is used in Java

  • Code reusability
  • Method overriding and to achieve runtime polymorphism

Common terms used in inheritance

  • Class: – A class refers to a group of objects which have common properties. We can also see it as a blueprint or template from which objects are created.
  • Child class/ subclass:- This is the class which inherits the properties and behaviors of the parent class. It is also known as a derived or extended class
  • Superclass/ Parent Class:- This is the class whose properties and features are inherited. It is also known as the base class
  • Reusability:- it is the process of reusing the methods and fields of the existing class in a newly created class. It allows you to use the same methods and fields that have already been defined in the previous class.

The syntax of inheritance in Java

Class Subclass-name extends Superclass-name

{

//methods and fields

}

The Keyword extends indicates that a new class is being made and it derives the properties of an existing class.

Types of inheritance in Java

There are three types of inheritance in Java:

  • Single Inheritance

When a class inherits another class, we call it single inheritance.

  • Multilevel Inheritance

Multilevel inheritance is used when we have a chain of inheritance.

  • Hierarchical Inheritance

This is when two or more classes inherit a single class

Polymorphism in Java

The ability of an object to take many forms is called polymorphism. In Object-Oriented Programming, polymorphism happens when a parent class reference is used to refer to an object in a child class. In Java, an object is considered polymorphic if it can pass more than one IS-A test. All Java objects are polymorphic because they pass the IS-A test for the class object and their type.

You should always remember that it is only possible to access an object through a reference object. There can only be one type of reference variable. The type of reference variable cannot be changed once it has been declared.

Moreover, we can reassign a reference variable to other objects provided that it has not been declared.

Example:

Public interface Herbivores { }

Public class Mammal { }

Public class cow extends mammal implements herbivore {}

In the above example, the class cow is regarded as polymorphic because it has multiple inheritances. The following statements are correct for the example above:

  • A cow IS-A mammal
  • A cow IS-A herbivore
  • A cow IS-A Cow
  • A cow IS-A Object

Runtime Polymorphism in Java

We can also refer to runtime polymorphism as dynamic method dispatch. It is the process where an overridden method is resolved at runtime instead of compile-time. In runtime polymorphism, the overridden method is called using the reference variable of a superclass. The object being referred to by the reference variable determines the method to be called.

  • Method overriding

This is when a derived class has a definition for one of the member functions of the base class.

To understand this process better, you first have to be familiar with upcasting.

Upcasting

It is when the reference variable of a parent class refers to the object of a child class.

Reference variable of parent class                                              Object of child class

Class M { }

Class K extends M{}

M m=new k(); //upcasting

The reference variable of an interface type or class type can be used in upcasting:

Interface I { }

Class M { }

Class K extends M implements I { }

Compile-time polymorphism

Also referred to as static polymorphism, compile-time polymorphism occurs when there is operator or function overloading.

  • Method overloading

Functions are said to be overloaded when there are several functions with the same name but different parameters. This can happen because of a change in the type of arguments in the number of arguments.

  • Operator overloading

The option of operator overloading is also available in Java. To concatenate two strings, we can make the operator ‘+’ for string class.

Get Java programming homework help if  you need exceptional help with your Java inheritance assignments.