Abstraction and Serious Polymorphism in Java

Pasan Kamburugamuwa
5 min readOct 14, 2021

--

Polymorphism in Java is the ability of an object to take many forms. To simply put, polymorphism in java allows us to perform the same action in many different ways. So before we go into the polymorphism, let’s have a look at abstract classes in java.

Some classes should not be initiated.

When you are designing your class inheritance structure, you have to decide which classes are abstract and which are concrete. Concrete classes are those that are specific enough to be initiated. A concrete class just means it’s OK to make objects of that type.

To make a class an abstract class, you need to put abstract keywords before the class declaration.

abstract class Canine extends Animal {
public void roam() { }
}

The Compiler won’t let you instantiate an abstract class.

An abstract class means you can not create a new instance of that class. You can still use that abstract class as a declared reference type for the purpose of polymorphism, but you don’t have to worry about somebody making objects of that type.

An example is taken from Head First Java — Kathy Sierra

So if we compile this class using,

javac MakeCanine.java

This will result in an error which is mentioning, Canine is abstract, cannot be instantiated.

An abstract class has virtually no use, no value, no purpose in life unless it is extended.

Abstract vs Concrete Classes

There is a significant difference between abstract classes and concrete classes in java. So let’s have a quick look at this.

  1. Modifier: An abstract class is declared using an abstract modifier. The concrete class should not be declared using abstract keywords, on doing so, it will also become an abstract class.
  2. Instantiation: An abstract class cannot be instantiated directly, i.e. object of such class cannot be created directly using a new keyword. An abstract class can be instantiated either by a concrete subclass or by defining all the abstract methods along with the new statement.
abstract class AbstractClass {
abstract void displayMessage();
}

public class JavaApplication {
public static void main(String[] args)
{
AbstractClass AC = new AbstractClass() {
void displayMessage()
{
System.out.println("Calling the displayMessage in abstract class");
}
};
AC.displayMessage();
System.out.println("This is the message");
}
}

Here, the abstract class is initiated by a concrete subclass and this allows to call the method inside that class.

So given below is a summary of the difference between the abstract class and concrete class.

Image is taken from https://www.geeksforgeeks.org/

Abstract Methods

An abstract class means the class must be extended, an abstract method means the method must be overridden. You might decide that some behaviors in an abstract class don’t make any sense unless they’re implemented in an abstract class don’t make any sense unless they’re implemented by a more specific subclass.

An abstract method has nobody!

In abstract methods, there can not have a method body and you need to override the method in order to have a method body.

public abstract void calculate();

You cannot have an abstract method in a non-abstract class as you need to declare the class as an abstract class too.

Polymorphism in action.

There are two types of polymorphism in java named, compile-time polymorphism and runtime polymorphism. We can perform polymorphism in java by overloading and overriding methods. So any Java object that passes more than one IS-A test will be polymorphic, and all Java objects are polymorphic as they have passed the IS-A test for their own type as well as the class Object.

Polymorphism means many forms.

There are two types of polymorphism in java namely, runtime polymorphism and compile-time polymorphism. We can perform polymorphism in java by method overloading and method overriding.

So let’s have a look at Compile-time polymorphism first.

Compile-time polymorphism

This is also known as static polymorphism. This can be achieved by java overloading. Below is a simple example of compile-time polymorphism.

class Calculator
{
int multiply(int a, int b)
{
return a*b;
}
int multiply(int a, int b, int c)
{
return a*b*c;
}
}

public class DemoApp
{
public static void main(String args[])
{
Calculator obj = new Calculator();
System.out.println(obj.multiply(10, 20));
System.out.println(obj.multiply(10, 20, 30));
}
}

So here have two definitions of the same method multiply() which multiply method would be called is determined by the parameter list at the compile time.

Runtime polymorphism

This is also known as dynamic polymorphism as this is a process in which a call to an overridden method is resolved at runtime. So let’s have a real-world example of runtime polymorphism here.

Employee class is the parent class and Manager class is the child class. The child class is overriding the method calculateSalary() of the parent class. In this example, we have a child class object assigned to the parent class reference so, in order to determine which method would be called, the type of the object would be determined at run-time.

class Employee{
public void calculateSalary(){
System.out.println("Salary calculation- overridden method");
}
}
public class Manager extends Employee{

public void calculateSalary(){
System.out.println("Salary calculation- overridden method");
}
public static void main(String args[]){
Employee obj = new Manager();
obj.calculateSalary();
}
}

When the overridden method is called through a reference of the parent class, then the type of the object determines which method is to be executed in runtime JVM. So the decision is taken at runtime and that’s why this is calling runtime polymorphism.

So this is all about the polymorphism in java. Hope you understand what is and when to use polymorphism. See you in the next tutorial. Thank you !

--

--