Inheritance and Polymorphism in java

from Head First Java chapter 07

Bavatharany Mahathevan
4 min readOct 17, 2021

As well known Java is an Object-Oriented Programming Language. One of the oop concepts is Inheritance. The meaning of inheritance in java is the processing of inheriting the public and protected property such as the instance variables and methods of a class by one or more classes automatically.

By using inheritance, we can reuse the code and avoid code duplication.

A class that inherits from another class is known as a subclass and a class that allows its own property to be inherited is known as a superclass.

So we can say like, the subclass inherits from the superclass, and in java keyword, subclass extends superclass. Another point is that when we inherit a class, there will be a relationship is built known as the IS-A relationship.

Eg : circle extends Shape, Equilateral extends Triangle

Subclass IS-A superclass. keep in mind when you want to do coding with inheritance, there should be an IS-A relationship between the subclass and superclass. If not, there is no inheritance and does not make sense.

And, there is also HAS-A relationship which is built between in class and objects.

Eg: Circle IS A Shape and Has A radian.

Q&A : What about the class and interface relationship?

if Class implements Interface :- class is like an interface. but Is like A is same as IS- A.

When you design your application before coding, you definitely will give attention to the objects and behaviours, if some objects have common, abstract characteristics, then we can create a new class (supper class)which can be able to extend by the other classes and add all these behaviour and state(attributes)in it.

In case, there will be a class with the same behaviour but has some specific requirements than others, the class also can extend the superclass and override the methods.

We don't need to override instance variables because it does not give any behaviour, but the subclass can use these instance variables by giving any values.

There are several types of inheritance in java: single level, multi-level hierarchical multiple level and hybrid.

In java, multiple levels and hybrid types of inheritance are achieved by using the interface.

single level

When a class inherits from another class is known as single inheritance.

Circle extends the Shape class.

Multi-Level

When a class inherits from another class which inherits from another class .it is like a chain of inheritance known as multi-level inheritance.

Ex: Equilateral extends Triangle and Triangle extends the Shape class.

Hierarchical Level.

When several classes inherit a base class known as Hierarchical inheritance. It can containers multi-level inheritance also.

Our above class diagram is at a Hierarchical Level inheritance.

The least subclass is able to access all its superclass methods, attributes that do not matter which level it is in the inheritance tree. at the same time, the superclass can not access the methods of the subclass.

There are three situations that prevent extending the superclass

  1. when superclass is a private class
  2. when superclass with final access modifier
  3. when superclass has only private constructors

Method overriding

A subclass that inherits a method from a superclass with the same method signature and different implementation is known as method overriding.

So that, you can create overloading by changing the number of arguments or changing the data type of arguments. the method overloading increases the readability of the code.

It provides run time polymorphism.

The thing that you have to consider when designing method overriding

  1. The method must have the same name and same arguments as in the superclass
  2. The method must have the same return type as the superclass.
  3. The method must have the same accessibility as a method of superclass or more, can not be less accessible by changing access modifiers.
  4. There must be an IS-A relationship between the two classes.
  5. can not override the static method.

Eg: from or class diagram, the method area() with one parameter is overriding in both Shape and Circle class while area() with two arguments also overriding in the Shape and Triangle class.

Method overloading

Within a class or subclass have multiple methods with the same name and different argument lists is known as method overloading. It provides the compiler time polymorphism in java.

The thing that you have to consider when designing method overloading

1. The methods must have the same name

2. Each method must be with different arguments

3. Can not change only the return type.

4 . Any method can be overloaded including the main method. but the JVM gives the priority to the method which has arguments String[] args.

Eg: from the above class diagram, the method area() is overloading in Shape class.

In java, Class Object is the superclass of everything. It means every class you write extends the Object class internally. Don't get confused about that so will java support multiple inheritances. the compiler will check who is the top-level superclass and didn't extend any classes then make the class extends the Object class.

Any class that does not extend another class explicitly, then the class will extend the Object class implicitly.

Polymorphism in Java

Having a clear picture of the concept of inheritance helps you to understand the concept of polymorphism quickly .because polymorphism is interconnected with inheritance method overloading and overriding.

Polymorphism is the process of allowing an object to take many forms.

Shape[] shapes = new Shape[2];

Here, The Shape array object will hold any of its subclasses. like:

shapes[0] = new Circle();

shapes[1] = new Equilateral(); calling the methods on this object provide the polymorphism.

In java, there are two types of polymorphism named Compile time polymorphism that is achieved by method overloading and Run time polymorphism that is achieved by method overring.

--

--

No responses yet