Understanding Polymorphism in Java

polymorphism in javaJava is an object-oriented language. Foundations of any object oriented language are laid on three pillars: Polymorphism, Inheritance and Encapsulation. Inheritance refers to the ability of a class to extend another class without having to rewrite the code in the parent class. Inheritance fosters code reusability and structuring. Encapsulation refers to the process of packaging related data together at a secure place. This is done in OOP languages via access modifiers. The third and most fascinating accept of any object oriented language is polymorphism that refers to achieving multiple functionality through same method interface. Polymorphism has been discussed in detail in the following section.

For more in-depth Java tutorials, take a course at Udemy.com.

What is Polymorphism in Java?

Polymorphism has been derived from a biological term that means ability of an organism to adopt multiple forms. In Java, polymorphism refers to the ability of referring and processing multiple objects and classes through a unified interface. Any object that satisfies multiple IS-A relationships is a candidate of being implemented as a polymorphic object. For instance, the object of class Square IS-A Square; It IS-A Shape as well and It IS-An object as well. It means that Object and Shape can be declared parent classes of child Square classes. Therefore, a common interface Object can be used to refer to all objects of Square class. Also, Shape interface can also be used to refer to all objects of Square class and so on. This is due to the IS-A relationship of Square with Object and Shape classes.

How Polymorphism is Implemented in Java

There are two types of polymorphism in Java: Static Polymorphism and Dynamic Polymorphism.

Static Polymorphism

The static polymorphism (also known as compile time polymorphism) is implemented via overloaded methods of a class. Method overloading refers to multiple methods with same name having different number or types of parameters. When the overloaded method is called, Java compiler checks itself that which method has to be invoked that matches the parameters of the called method. The following example demonstrates the concept of Static Polymorphism.

Consider a Maths class that contains three Add methods. First method returns an integer and takes two integer types variable. The second Add method also returns and integer but takes three integer types variables and the third Add method returns a String and takes two string type parameters. Thus, all the Add methods have different number of types of parameters. These three Add methods are called overloaded methods. The definition of the Maths class looks like this:

public class Maths {
public int Add (int num1, int num2)
{
return num1 + num2;
}
public int Add(int num1, int num2, int num3)
{
return num1 + num2 + num3;
}
public String Add (String s1, String s2)
{
return s1 + s2;
}
}

If the Add method is called on the object of Maths class by passing two integer type parameters, the Java compiler would look for the Add method in the class Maths that takes two integer type parameters. That method is invoked. If the Add method is called by passing three integer type parameters, the corresponding overloaded Add method with three integer type parameters would be called and if Add method with two string parameters is called, the compiler will automatically call that Add method. The following code snippet demonstrates the three calls to Add method.

Maths m1 = new Maths();
int sum = m1.Add(50, 100);
System.out.println (sum); 
sum = m1.Add(50, 100, 90);
System.out.println (sum);
String str = m1.Add("Udemy ", "Tutorials");
System.out.println (str);

The output of the above code will be:

150

240

Udemy Tutorials

 

With same method name, three different types of functionalities are achieved. This is the basic principle of static polymorphism.

 

Interested in learning more about Java? Take a course at Udemy.com.

Dynamic Polymorphism

Dynamic Polymorphism is implemented in Java using overridden methods. Method overrides refer to the process of providing the same method name but with different parameters.

For instance, there is a parent Shape class that contains a method Draw. A derived Square class extends the Shape class and implements its own Draw method which means that Square class overrides the Draw method of the parent Shape class. There is another derived class of Shape class which is a Circle class and this class also overrides the Draw method of the parent class.

Now, if the reference of the Shape class is used to refer to the object of the Square class and then Draw method is called, the Draw method of the Square class will be called. However, if Shape class object is used to refer to the object of Circle class and then Draw method is called on that, the Draw method of the circle class will be called. In other words, different functionality of Draw method would be achieved through a single unified interface. The following code snippet demonstrates this concept.

The definition of the parent Shape class looks like this:

public class Shape {
public void Draw()
{
System.out.println ("A shape is being drawn");
}
}

The Square and Circle classes extend this Shape class as follows:

public class Square extends Shape
{
public void Draw()
{
System.out.println ("A Square is being drawn");
}
}

public class Circle extends Shape
{
public void Draw()
{
System.out.println ("A Circle is being drawn");
}
}

Now, if three reference variables of Shape class are initialized with objects of Shape, Square and Circle classes, and then Draw method is called on each variable the result would be different. Following code demonstrates this:

Shape s1 = new Shape();
Shape s2 = new Square();
Shape s3 = new Circle();
s1.Draw();
s2.Draw();
s3.Draw();

The Shape s1 refers to the Object of class Shape; s2 refers to the object of class Square and s3 refers to the object of class circle. Though the three variables are of single Shape class, however the object to which they refer are different. Therefore, the Draw method of the object to which these variables refer would be called. The output of the above code will be as follows:

A Shape is being drawn

A Square is being drawn

A Circle is being drawn

 

To learn more about Java, check out online Java courses at Udemy.com