Overloading and overriding methods are two fundamental topics in Java object-oriented programming. Both notions are connected to the operation of Java methods, yet they serve different functions.
Method Overloading:
Method Overloading is a technique that allows you to declare numerous methods with the same name but different parameters in the same class. In other words, you can have several methods with the same name in a class as long as they take different parameters.
IF
A class has multiple methods having same name but have different parameters. This is called method overloading.
In method overloading we do not need parent-child relationship.
For example, we have a class name Calculator, and we want to define a method named subtraction() that can subtract two integers or doubles. We can do this using method overloading as shown below:
package polymorphism1;
class Calculator{
public int subtraction(int n1,int n2,int n3) {
return n1-n2-n3;
}
public int subtraction(int a,int b) {
return a-b;
}
}
public class polymorphism {
public static void main(String[] args) {
Calculator obj=new Calculator();
int k1= obj.subtraction(55,10,10);
System.out.println(k1);
}
}
Output: 35
The Calculator class has two methods named subtraction. Both methods have the same name but different parameters.
The first method subtraction takes three integer arguments n1, n2, and n3 and returns their difference after performing subtraction operation.
The second method subtraction takes two integer arguments a and b and returns their difference after performing subtraction operation.
In the main method of the polymorphism class, an object of Calculator class is created.
Then, the subtraction method with three integer arguments 55, 10, and 10 is called on the Calculator object, and its return value is stored in an integer variable k1.
Finally, the value of k1 is printed on the console using System.out.println(k1).
Since the subtraction method is overloaded, the compiler determines which version of the method to call based on the number and types of arguments passed to it. In this case, the version of the subtraction method that takes three integer arguments and returns the value 35.
Now,
package polymorphism1;
class Calculator{
public int subtraction(int n1,int n2,int n3) {
return n1-n2-n3;
}
public int subtraction(int a,int b) {
return a-b;
}
}
public class polymorphism {
public static void main(String[] args) {
Calculator obj=new Calculator();
int r1= obj.subtraction(55,10);
System.out.println(r1);
}
}
Here, the subtraction method is overloaded, the compiler determines which version of the method to call based on the number and types of arguments passed to it. In this case, the version of the subtraction method that takes two integer arguments is called and returns the value 45 (i.e., the difference of 55 and 10).
Method Overriding: (Need parent child relationship)
Method overriding is a technique in Java that allows you to create a new implementation of an existing method in a subclass. When a subclass defines a method with the same signature (name and parameters) as a method in its super class, the sub class’s method is said to override the super class’s method.
package polymorphism1;
class nepal{
public void call() {
System.out.println("Mount Everest");
}
}
public class polymorphism2 {
public static void main (String args[]) {
nepal obj=new nepal();
obj.call();
}
}
Here, Nepal, which has a method call, and Polymorphism2, which has a main function that creates an object of Nepal and calls its call method.
The call function in the Nepal class uses the System.out.println method to print the string “Mount Everest” to the console.
The new keyword is used in the polymorphism2 class to create an object of the Nepal class, which is then assigned to a variable named obj. The call method is then called on the obj object using dot notation (obj.call()).
When you run the polymorphism2 program, the nepal class’s call method is called, and the string “Mount Everest” is printed to the console.
Now, if we create another class as follows then:
package polymorphism1;
class nepal{
public void call() {
System.out.println("Mount Everest");
}
}
class pokhara extends nepal{
}
public class polymorphism2 {
public static void main (String args[]) {
pokhara obj=new pokhara();
obj.call();
}
}
Output: Mount Everest
Here, a class nepal with a method call, another class pokhara which extends the nepal class, and a class polymorphism2 with a main method that creates an object of pokhara class and calls its call method.
The nepal class has a single method call which prints the string "Mount Everest" on the console using System.out.println method.
The pokhara class extends the nepal class, which means it inherits all the methods and variables of the nepal class. Since pokhara does not define its own implementation of call method, it uses the implementation of the call method from its superclass nepal.
In the main method of the polymorphism2 class, an object of pokhara class is created using the new keyword and assigned to a variable named obj. Then, the call method is invoked on the obj object using the dot notation (obj.call()).
When you run the polymorphism2 program, the call method of the nepal class (inherited by the pokhara class) is executed, and it prints the string "Mount Everest" on the console.
Again, if we create another method name lake() then:
package polymorphism1;
class nepal{
public void call() {
System.out.println("Mount Everest");
}
public void lake() {
System.out.println("Fewa lake");
}
}
class pokhara extends nepal{
}
public class polymorphism2 {
public static void main (String args[]) {
pokhara obj=new pokhara();
obj.call();
obj.lake();
}
}
Output:
Mount Everest
Fewa lake
Here, a class nepal with two methods call and lake, another class pokhara which extends the nepal class, and a class polymorphism2 with a main method that creates an object of pokhara class and calls its call and lake methods.
The nepal class has two methods call and lake. The call method prints the string "Mount Everest" on the console using System.out.println method and the lake method prints the string "Fewa lake" on the console using System.out.println method.
The pokhara class extends the nepal class, which means it inherits all the methods and variables of the nepal class. Since pokhara does not define its own implementation of call and lake methods, it uses the implementation of the call and lake methods from its superclass nepal.
In the main method of the polymorphism2 class, an object of pokhara class is created using the new keyword and assigned to a variable named obj. Then, the call and lake methods are invoked on the obj object using the dot notation (obj.call() and obj.lake()).
When you run the polymorphism2 program, the call method of the nepal class (inherited by the pokhara class) is executed, and it prints the string "Mount Everest" on the console. Then the lake method of the nepal class (inherited by the pokhara class) is executed, and it prints the string "Fewa lake" on the console.
Now, if we change the program like follows then:
package polymorphism1;
class nepal{
public void call() {
System.out.println("Mount Everest");
}
public void lake() {
System.out.println("Fewa lake");
}
}
class pokhara extends nepal{
public void call() {
System.out.println("Gautam Buddha");
}
}
public class polymorphism2 {
public static void main (String args[]) {
pokhara obj=new pokhara();
obj.call();
obj.lake();
}
}
The nepal class has two methods call and lake. The call method prints the string "Mount Everest" on the console using System.out.println method and the lake method prints the string "Fewa lake" on the console using System.out.println method.
The pokhara class extends the nepal class and overrides the call method. This means that pokhara defines its own implementation of the call method instead of using the implementation inherited from its superclass nepal. In this case, the call method in the pokhara class prints the string "Gautam Buddha" on the console using System.out.println method.
In the main method of the polymorphism2 class, an object of pokhara class is created using the new keyword and assigned to a variable named obj. Then, the call and lake methods are invoked on the obj object using the dot notation (obj.call() and obj.lake()).
When you run the polymorphism2 program, the call method of the pokhara class is executed (not the call method of its superclass nepal), and it prints the string "Gautam Buddha" on the console.
Then, the lake method of the nepal class (inherited by the pokhara class) is executed, and it prints the string "Fewa lake" on the console.
This demonstrates an example of method inheritance and method overriding in Java.
Comentarios