Method Overloading and Method Overriding

Method Overloading and Method Overriding

Method overloading and method overriding are two popular polymorphism concepts in Java programming. They help Java users define multiple, universally ordered user-defined class methods with the same name. Multiple user-defined class methods are called in different ways in a program, helping Java programmers achieve multiple class objectives.

Method Overloading and Method Overriding

So, let’s take a closer look at class method overloading and method overriding concepts in Java programming.

Method Overloading in Java Classes.

In Java, method overloading is performed when multiple custom class methods with the same name are created in a user-defined class program. These concepts are defined differently in Java user-created class methods.

In which user-created classes declare different parameter numbers (different class parameter lists), etc. The data type of custom parameters declared in the existing class (different data types), etc.

The order of method variable parameters declared in the existing class (in case of different data types).

Features of method overloading in a Java class.

Java class method overloading allows Java programmers to define or create multiple versions of a declared class method within a single class. In contrast, method overloading allows them to be defined with multiple individual class parameter arguments.

Return type in a class program does not play a significant role in method overloading, where the user-defined class variable parameter numbers or data types must be defined differently in the method signature declared in the class.

Method overloading is managed at compile-time. This process is known as compile-time polymorphism or static polymorphism.

Example of method overloading in a Java class.

class Calculation {

// here we define a method Overloaded method to total two integers’ data parameter

public int total(int p, int q) {

return p + q;

}

// here we use method Overloaded method to total three integers’ data type value

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

return p + q + r;

}

// here method Overloaded method used to total two doubles integer value

public double total(double p, double q) {

return p + q;

}

// here Overloaded method used to total one integer and one double integer data type

public double total(int p, double q) {

return p + q;

}

}

public class Main {

public static void main(String[] args) {

Calculation count = new Calculation();

System.out.println(count.total(7, 9)); // Here it calls total(int, int) data type parameter

System.out.println(count.total(9, 8, 3)); // Here it calls total(int, int, int) data type parameter

System.out.println(count.total(3.9, 7.9)); // Here it calls total(double, double) data type parameter

System.out.println(count.total(7, 5.8)); // Here it calls total(int, double) data type parameter

}

}

Method overloading in a Java class explanation.

  • Here, method overriding in the Calculation class is defined as multiple methods named count, while these are represented with different data type parameter lists.
  • count(int, int) adds two integer values ​​to a class method.
  • count(int, int, int) adds three different integer values ​​to a class method.
  • count(double, double) adds two double integer values ​​to a class method.
  • count(int, double) adds one integer and one double value to a class method.

Java Method Overriding in Classes.

Method overriding in a Java class is performed when a user-defined subclass of a class provides a special implementation for a class method that is already defined in its superclass. Here, the method signature, class name, return data type, and class data parameter variables of the overridden class method in the subclass are defined in the same way as the method in the parent class. Here the declared subclass can have its own version of the method, while the method name of the class remains the same as defined.

Features of Java Method Overriding.

  • In a Java class program, the signature of a subclass method must be identical to that of a superclass method (e.g., the same method name, return data type, and class data parameter variables used).
  • Overriding a method declared in a class implements the concept of runtime polymorphism (dynamic polymorphism). This means that the class in which the method is invoked is determined at runtime based on the class object’s data type.
  • In a user-defined superclass, no class methods declared should be final, static, or private, as these cannot be manually overridden within the class if needed.
  • The @Override annotation in an overridden class is used to indicate that an existing class method is being overridden in the current class. This is an optional task, and it helps beginner Java programmers avoid programming mistakes.

Example of Java class method overriding.

class Course {

/ Here we define a course method in the superclass

public void select_course() {

System.out.println(“Course, select your desired course”);

}

}

class Java extends Course {

/ Here we override the Java method in the subclass

@Override

public void select_course() {

System.out.println(“You select Java”);

}

}

class Python extends Course {

/ Here we override the Python method in the subclass

@Override

public void select_course() {

System.out.println(“You select Python”);

}

}

public class Main {

public static void main(String[] args) {

Course course = new Course();

Course java = new Java(); // Here we are upcasting

Course python = new Python(); // Here we are upcasting

course.select_course(); // Here it is calling the Course select_course() function method

java.select_course(); // Here it is calling the Java select_course() due to overriding

python.select_course(); // Here it is calling the Python select_course() due to overriding

}

}

Java class method overriding explained.

  • Here, in the class program, the select_course() method in the Course superclass is overridden in the Java and Python subclasses.
  • Whereas, in the Java and Python classes, the @Override annotation is used to indicate that the select_course() method is being overridden.
  • The class method select_course() is called on an object of type Course, but it invokes the overridden method of the Java and Python classes. This represents runtime polymorphism behavior in the existing class program.

Leave a Reply