Let us first look into what the name suggests at first glance. “Overloading” means: putting some extra burden on anybody’s original functionality, right? Meanwhile, “Overriding” means providing new functionality in addition to anyone’s original functionality. In this article, we will look at Overloading and Overriding in Java in detail. Yes, programmatically implementing these in Java follows the same approach. Let us have a look at them one by one.
When a java class has multiple methods with the same name but with different arguments, we call it Method Overloading. By keeping the name the same, we are just increasing the readability of the program code. For example, suppose we need to perform some addition operation on some given numbers. Let us say the name of our method is “addition()”. Here, addition can be done between two, three, or more. Hence, depending on how many numbers will be involved in the additional operation, we can change the function’s arguments (or parameters). But, instead of this, if you would write different methods for the different number of arguments, it would be difficult to recognize as the name would be different. Hence, by overloading, we are achieving better readability of our code. So now the question is, how will we achieve overloading?
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Let’s look at those ones by one with example codes.
So here, we will do additional operations on some numbers. For this, let us create a class called “AdditionOperation”. Inside that class, let’s have two methods named “addition()”. In one of those methods, we will add two numbers. In the other, we will add three numbers. We will achieve this by changing the number of parameters in those methods, but we will keep the same name. In this way, we overload the method “addition()” here.
Code:
public class AdditionOperation { static int addition(int num1,int num2){return num1+num2;} //function declarationand definition for addition of two numbers static int addition(int num1,int num2,int num3){return num1+num2+num3;} //function declarationand definition for addition of three numbers public static void main(String args[]) { system.out.printin(addition(35,36)); //method overloading system.out.printin(addition(35,36,37)); //method overloading, we are calling same methods but for different number of arguments. } }
Output:
Here, we will do an addition operation on different types, for example, between integer and double types. For this, let us create a class called “AdditionOperation”. Inside that class, let us have two methods named “addition()”. In one of those methods, we will add two integers. In the other, we will add two doubles. We will achieve this by changing the type of parameters in those methods, but we will keep the same name. In this way, we overload the method “addition()” here.
Code:
public class additionOperation { static int addition(int num1,int num2){return num1+num2;} //function declarationand definition for addition of two numbers static double addition(double num1,num2){return num1+num2;} //function declarationand definition for addition of three numbers public static void main(String args[]) { system.out.printin(addition(35,36)); //method overloading system.out.printin(addition(35.5,36.6)); //method overloading, we are calling same methods but for different type of arguments. } }
Output:
Points to be Noted for Overloading
Code:
//Parent or Super class class Parent { public void display() { system.out.printin("Hello, I am from parent class"); } } //Child or sub class class Sub extends Parent { //Below method overrides the Parent display() method // @override public void display() { system.out.printin("Hello, I am from child class"); } } //Driver class public class Overriding { public static void main?(String args[]) { Parent superObject = new Parent (); superObject.display(); // Super class method is called Parent subObject = new Sub(); subObject.display(); //Child class method is called by a parent type reference: this is functionality of method overriding Sub subObject2 = new Sub(); //Child class method is called by a child type reference subObject2.display(); } }
Output:
Limitations in method Overriding:
Points to be Noted for Overriding
This concludes our learning of the topic “Overloading and Overriding in Java”. Write the codes mentioned above in the java compiler and check the output. Learning codes will be incomplete if you do not do hands-on by yourself, enhancing your coding skills. Happy coding!!
The above is the detailed content of Overloading and Overriding in Java. For more information, please follow other related articles on the PHP Chinese website!