Compile-time vs Runtime Polymorphism

Compile-time vs Runtime Polymorphism

The class method polymorphism concept in Java programming helps Java programmers to deal with various types of class method superclass and subclass data type parameter elements based on the defined class object, in which they are processing the parameter object arguments defined in the current class. The class polymorphism concept in Java is one of the popular concepts of object-oriented programming (OOP), and polymorphism features help Java programmers to create or achieve flexibility and reusability in program source code.

Compile-time vs Runtime Polymorphism

The polymorphism concept in Java programming can be divided into two forms.

  • The first is compile-time Java class polymorphism, also known as static polymorphism.
  • The second is runtime Java class polymorphism, also known as dynamic polymorphism.

So, let’s explore these two types of polymorphism concepts in Java classes in detail.

Compile-time Java class polymorphism/static polymorphism.

Compile-time polymorphism in Java class programming is triggered when a class method executed within a Java-defined class is automatically resolved during the compilation phase. This polymorphism concept is typically achieved through class method overloading and operator overloading.

In Java class programming, method overloading is a prime example of compile-time polymorphism. In method overloading, a user-defined class can have multiple class methods with the same name defined, while the variable parameters declared in these defined classes can have different numbers or data types.

Features of Compile-time Java Class Polymorphism.

  • The method overloading concept in Java class programming is a prime example of compile-time polymorphism.
  • The decision to call which method in the declared or defined class is made at compile-time based on the program’s method signature.
  • The return class data type does not play an important role in the method overloading process.

Example of compile-time polymorphism/method overloading in Java.

class Calculation {

// Here, overloaded methods with different parameter types or number of parameters

public int total(int p, int q) {

return p + q;

}

public double total(double p, double q) {

return p + q;

}

public int total(int p, int q, int r) {

return p + q + r;

}

}

public class Main {

public static void main(String[] args) {

Calculation count = new Calculation();

System.out.println(count.total(7, 9)); // Here it calls the total(int, int) class method

System.out.println(count.total(3.9, 7.9)); // Here it calls the total(double, double) class method

System.out.println(count.total(9, 1, 8)); // Here it calls the total(int, int, int) data type method

}

}

Explaining compile-time polymorphism.

  • In this program, the total class method is automatically overloaded with a different variable defined in the class parameter list.
  • Where the correct method version defined in the class is called based on the number and data type of parameter arguments passed, and this decision is made during the compilation phase.

Runtime Java Class Polymorphism/Dynamic Polymorphism.

Runtime polymorphism in a Java class program occurs when the class method to be executed is resolved during the program’s runtime execution phase, rather than during the program’s compile-time processing phase. This activity is possible due to the concept of overriding in Java class methods.

In Java method overriding, a subclass provides its own implementation of a class method previously declared in its superclass. The decision to invoke an existing class method is made at runtime based on the actual object data type (not the reference type).

Features of Runtime Java Class Polymorphism.

  • Method overriding in a Java user-defined class is one of the most common methods for implementing runtime polymorphism.
  • In this process, the decision about which class method to call is made at runtime based on the actual class object data type.
  • Where the method signature in the user-defined subclass must be the same or exactly similar to the method signature in the superclass.

Example of runtime Java class polymorphism/method overriding.

class Course {

public void select_course() {

System.out.println(“Course, select_course”);

}

}

class Java extends Course {

@Override

public void select_course() {

System.out.println(“Java selected”);

}

}

class Python extends Course {

@Override

public void select_course() {

System.out.println(“Python selected”);

}

}

public class Main {

public static void main(String[] args) {

Course coursefirst = new Java(); // Course reference, but Java object created

Course coursesecond = new Python(); // Course reference, but Python object created

coursesfirst.select_course(); //here it calls Java select_course() method

coursesecond.select_course(); // here it’s calling the Python select_course() method

}

}

Runtime Java class polymorphism explanation.

  • In this program, the select_course() class method is overridden in both Java and Python classes.
  • The reference variable courrsefirst is of type Course, but it points to a Java object, and the reference variable coursesecond is of type Course, but it points to a Python class object.
  • In the Course class runtime process, the Java Virtual Machine (JVM) determines the actual class object type, whether Java or Python, and calls the correct select_course() method on the class.

Leave a Reply