21
NovWhat is Polymorphism in Java? Types of Polymorphism (Compile time & Run Time)
Polymorphism in Java - An Overview
Polymorphism is an important feature in Java that allows methods to do various tasks depending on the object that invokes them. In the word Polymorphism, "Poly" stands for many, and "Morph" stands for forms, thus, it means many forms. Polymorphism is divided into two subtypes compile-time polymorphism and run-time polymorphism. Compile-time polymorphism is performed through method overloading, and runtime polymorphism in Java is implemented through method overriding.
In this Java Tutorial, we will explore Polymorphism in Java, including the different types of polymorphism and the difference between method overriding and method overloading.
Build your dream career as a full stack developer—sign up for our Java Full Stack Developer Certification Training today!
Read More: Best Java Developer Roadmap 2024
What is Polymorphism in Java?
Polymorphism is an essential feature of Java and one of the essential pillars of object-oriented programming. It represents a single entity in multiple forms, such as constructor overloading and method overloading in Java. Java conditional statements play an important role in enabling developers to implement polymorphism effectively.
Different Types of Polymorphism in Java
There are two types of Polymorphism in Java, which are:
- Compile time Polymorphism in Java
- Run time Polymorphism in Java
Read More - Advanced Java Interview Questions
1. Compile time Polymorphism in Java
Compile time polymorphism in Java resolves the problem regarding compile time with the help of "Method overloading" and "Constructor overloading".
1. Method Overloading in Java
Method overloading in Java works for two or more methods or functions stored in the same class with the same name but different parameters and arguments.
Example of Method Overloading in Java
class Method_Overloading
{
//Method Overloading by changing the number of arguments (or parameters)
//Method 1
double figure(double l, double b) //two arguments or parameters
{
return (l*b);
}
double figure(double s) //one argument or parameter
{
return (s*s);
}
//Method 2
public static void main(String[] args)
{
Method_Overloading obj = new Method_Overloading();
System.out.println("Area of Rectangle: " +obj.figure(5.55, 6.78));
System.out.println("Area of Square: " +obj.figure(3.45));
}
}
Output
Area of Rectangle: 37.629
Area of Square: 11.90250000000002
2. Constructor Overloading in Java
Constructor overloading in Java when more than one constructor is declared inside a class but with different parameters. If an object in a class is created by using a new keyword, it generates a constructor in that class. In this Guide to Java, we'll explore how constructor overloading works through illustration in Java Online Editor and its significance in object-oriented programming.
Example of Constructor Overloading in Java
public class ScholarHat {
// function for adding two integers
void add(int a, int b) {
int sum = a + b;
System.out.println("Addition of two integers: " + sum);
}
// function for concatenating two strings
void add(String s1, String s2) {
String con_str = s1 + s2;
System.out.println("Concatenated strings: " + con_str);
}
public static void main(String args[]) {
ScholarHat obj = new ScholarHat();
// addition of two numbers
obj.add(10, 10);
// concatenation of two strings
obj.add("Operator ", "overloading ");
}
}
Output
Addition of two integer :20
Concatenated strings :Operator overloading
For learning more, refer to Constructor Overloading in Java
Subtypes of Compile-time Polymorphism
2. Runtime Polymorphism in Java
Runtime Polymorphism specifies the depiction of run time by using overriding.
1. Method Overriding in Java
This particular Java method redefines a superclass method by generating the same name, parameters, and data types in a subclass.
Example of Method Overriding in Java
//A Simple made up Instance to show method overriding in Java
class Waka_Waka_Song
{
Waka_Waka_Song()
{
System.out.println("Song--Initializing Waka Waka by Shakira...");
}
public void play()
{
System.out.println("Song-- Playing...");
}
}
//Spotify wants Waka Waka song on their Platform
class Spotify extends Waka_Waka_Song
{
Spotify()
{
System.out.println("Spotify...");
}
public void play()
{
System.out.println("Spotify-- Playing.. ");
}
}
//Amazon_Music wants Waka Waka song on their Platform
class Amazon_Music extends Waka_Waka_Song
{
Amazon_Music()
{
System.out.println("Amazon Music...");
}
public void play()
{
System.out.println("Amazon Prime-- Playing...");
}
}
//A User Playing songs on Spotify and then Amazon Music
class User
{
public static void main(String[] args)
{
System.out.println("Playing Waka Waka on Spotify... ");
Spotify s = new Spotify();
s.play();
System.out.println("\nPlaying Waka Waka on Amazon Music");
Amazon_Music p = new Amazon_Music();
p.play();
}
}
Output
Playing Waka Waka on Spotify…
Song - - Initializing Waka Waka by Shakira…
Spotify…
Spotify- - Playing . .
Playing Waka Waka on Amazon Music
Song - - Initializing Waka Waka by Shakira…
Amazon Music …
Amazon Prime- - Playing …
Read More - Difference Between Method Overloading And Overriding In Java
2. Java Operator Overloading
- Depending on the operands, operators like + in Java behave differently.
- Java's operator overloading permits polymorphism, in which operators behave differently depending on the kinds of operands, improving the readability of the code.
- Contrary to languages like C++, Java does not provide user-defined operator overloading.
- In Java, operators are predefined and have fixed behaviors based on the types of inputs. For user-defined classes or types, specific operator overloads are not permitted.
3. Subtype of Run-time Polymorphism
What are Polymorphism Variables?
- If a variable can refer to different values under different conditions, it is said to be polymorphic.
- The behavior of polymorphic variables in Java is represented by object variables (also known as instance variables).
- It's because a class's object variables can refer to both objects from that class and objects from its subclasses.
Example
class ProgrammingLanguage {
public void display() {
System.out.println("Welcome to ScholarHat.");
}
}
class Java extends ProgrammingLanguage {
@Override
public void display() {
System.out.println("This is our Polymorphism tutorial");
}
}
class Main {
public static void main(String[] args) {
// declare an object variable
ProgrammingLanguage pl;
// create object of ProgrammingLanguage
pl = new ProgrammingLanguage();
pl.display();
// create object of Java class
pl = new Java();
pl.display();
}
}
Output
Welcome to ScholarHat.
This is our Polymorphism tutorial
Why use Polymorphism in Java?
Java's polymorphism makes it easy to create methods that can correctly handle a wide variety of functions with the same name. We can use polymorphism to improve the consistency of our code.
Advantages of Polymorphism in Java
- Reusable classes can be created, tested, and implemented.
- Reusing old code helps programmers save time.
- Changes can be made without having an impact on the original code.
- Enables the storage of numerous data values in a single variable.
- Independent changes can be made to values that a subclass inherits from a superclass.
- The superclass and other subclasses are unaffected by changes to the subclass variable.
- Debugging is made simpler by fewer lines of code.
- Makes it easier for programmers to find and fix problems.
Characteristics of Polymorphism
Other than Method Overloading and Method Overriding, polymorphism exhibits a wide range of other characteristics. They consist of:
1. Coercion
- Coercion involves the implicit conversion of one object type into another, automatically avoiding type mistakes.
- The conversion of data types between various types is supported by programming languages like C, Java, etc.
- Conversions between data types can be implicit or explicit.
- The program does implicit type conversion, commonly referred to as coercion.
- When an integer operand is combined with a float operand, the compiler implicitly transforms the integer to a float to avoid type problems.
Example of Coercion in Polymorphism in Java
class Shape {
public void draw() {
System.out.println("Drawing a shape");
}
}
class Circle extends Shape {
@Override
public void draw() {
System.out.println("Drawing a circle");
}
}
class Main {
public static void main(String[] args) {
Shape shape = new Circle(); // Coercion of Circle to Shape
shape.draw(); // Calls the overridden draw() method in Circle
}
}
Output
Drawing a circle
2. Internal Operator Overloading
- Making an operator or symbol do several operations depending on the situation or the types of operands it works on is known as operator overloading.
- The same operator or symbol might behave in a variety of ways based on the operands in static polymorphism, which is related to this phenomenon.
- Java does not provide user-defined operator overloading, a feature that allows users to specify unique actions for operators with various operand types.
- Java internally overloads some operators, enabling them to function differently in some circumstances.
- To address certain programming requirements, operator overloading enables programmers to utilize operators or method names as if they were user-defined types.
- As an example, the Java '+' operator can be used for both addition (with operands of the same data type in Java) and string concatenation (with operands of the string type).
- The expressiveness and functionality of the code are improved by the flexibility of the operator usage.
Example of Internal Operator Overloading in Java
public class Vector {
private double x;
private double y;
// Constructor
public Vector(double x, double y) {
this.x = x;
this.y = y;
}
// Method to add vectors
public Vector add(Vector other) {
double newX = this.x + other.x;
double newY = this.y + other.y;
return new Vector(newX, newY);
}
// Method to multiply vector by a scalar
public Vector multiply(double scalar) {
double newX = this.x * scalar;
double newY = this.y * scalar;
return new Vector(newX, newY);
}
// Method to display vector
public void display() {
System.out.println("(" + this.x + ", " + this.y + ")");
}
public static void main(String[] args) {
Vector v1 = new Vector(2, 3);
Vector v2 = new Vector(1, 1);
// Add vectors
Vector sum = v1.add(v2);
System.out.print("Sum: ");
sum.display();
// Multiply vector by scalar
Vector scaled = v1.multiply(2);
System.out.print("Scaled: ");
scaled.display();
}
}
In the above code in the Java Compiler, the Vector class represents a 2D vector with x and y components. The class defines methods for vector addition (add) and scalar multiplication (multiply). These methods simulate the behavior of operators + and * respectively.
Output
Sum: (3.0, 4.0)
Scaled: (4.0, 6.0)
3. Polymorphic Variables or Parameters
- Polymorphic variables are represented in Java by object or instance variables.
- A class's object variables may display an IS– A polymorphic relationship with both their class and its subclasses.
- A variable that can store several types of values while a program is running is said to be polymorphic.
- When a class is declared in Java, parametric polymorphism enables field names to be associated with various types.
- Additionally, it makes it possible for method names to be linked to various argument and return types, improving the flexibility and reusability of code design.
The Difference between Method Overriding and Method Overloading
Method Overloading in Java | Method Overriding in Java |
It is a compile-time polymorphism. | It is a run-time polymorphism. |
It helps to increase the readability of the program. | It is used to give access to the specific implementation of the method which is already provided by its parent class or superclass. |
Method overloading occurs within the class only. | Method overriding is performed in two classes with inheritance relationships. |
It may or may not require inheritance. | It always needs inheritance. |
In this, methods must have the same name and different characteristics | In this, methods must have the same name and same characteristics. |
In this, the return type can or can not be the same, but you have to change the parameter. | In this, the return type must be the same or co-variant. |
Static binding is used in method overloading. | In method overriding dynamic binding is used. |
In this, Private and final methods can be overloaded. | In this, private and final methods can’t be overridden. |
The parameters should be different while doing method overloading. | The parameters should be the same in method overriding. |
Summary
In this comprehensive Java tutorial, the definition of polymorphism in Java including its type has been discussed vastly. How can you achieve runtime polymorphism has also been highlighted in this article. The definition of Runtime and Compile time Polymorphism with examples has been evaluated in this article, providing a comprehensive understanding of these concepts for Full Stack Java Course.