Updated March 17, 2023
Introduction to Overloading and Overriding in Java
Let us first look into what the name suggests at first glance. “Overloading” means: putting some extra burden on anybody’s original functionality, right? Whereas “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, in Java also, these are implemented in the same way programmatically. Let us have a look at that one by one.
Overloading in Java
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 numbers, three numbers, or more. Hence, depending upon 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 will 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?
Let’s look at those ones by one with example codes.
Method #1 – By modifying the number of parameters
So here, we will do additional operation 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 perform an addition of two numbers. In the other, we will perform the addition of three numbers. This we will achieve by simply changing the number of parameters in those methods, but we will keep the name the same. In this way, we are overloading 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:
Method #2 – By modifying the datatype
Here, we will do an addition operation on different types, for example, between integer type and double type. 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 perform the addition of two integers. In the other, we will perform the addition of two double. This we will achieve by simply changing the type of parameters in those methods, but we will keep the name the same. In this way, we are overloading 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
- Overloading in java is basically a “compile-time polymMethod Overloading in C#orphism”. Compile-time polymorphism in java is also called as “Static method Dispatch” or “Early binding”. So what do I mean by that jargon?
- As the name suggests, polymorphism is basically an ability to take many forms (poly: many, morph: form). So, here linking or binding of the overriden function and the object is done compile time. Hence it is called compile-time polymorphism.
- Basically, the binding of function to object is done early before run time (i. e., during compile time); hence, it is also named “Early binding”.
- Static dispatch is a type of polymorphism or method dispatch which tells how java will select which functionality of the method will be used in compile time. (I mean, whether it will perform the addition of two numbers or three numbers in our coding example). So the name is also known as the Static method Dispatch.
Overriding in Java
- When a java subclass or child class has a method that is of the same name and contains the same parameters or arguments and similar return type as a method that is present in its superclass or parent class, then we can call the method of the child class as an overridden method of the method of its parent class.
- For example, suppose we need to perform some display operation according to its class type. If I call the method of a parent class, it will display a message defined in a parent class. But, when we call the child class’s method, it will override the display message of its parent class and show the display message, which is defined inside the method of the child class. Hence depending on which display we need to show, we can call the related class (parent or child). Here we are not changing the method name, argument and return type. We are just changing the functionality of the method in child class. But, instead of this, if we do not do overriding, i. e. we don’t give the specific implementation of the child method, then while calling the method, it will display the same message as present in a parent class.
- While writing code, we will use @Override annotation before the method to be overridden. @Override annotation tells the compiler that the method is supposed to override a method that was declared in a superclass. Although it is not mandatory to use this, it helps to prevent errors. If a method that is annotated with @Override fails to override a method, the compiler generates an error.
Rules for Java Method Overriding
- The method must have the same name as in the parent class
- The method must have the same parameter as in the parent class.
- There must be an IS-A relationship (inheritance).
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:
- Private methods of the parent class cannot be overridden.
- Final methods cannot be overridden
- Static methods cannot be overridden
Points to be Noted for Overriding
- Overriding in java is basically “Run time polymorphism”. Run time polymorphism in java is also called as “Dynamic method Dispatch” or “Late binding”. So what is meant by that jargon?
- As the name suggests, polymorphism is basically an ability to take many forms (poly: many, morph: form). So, here call to an overriden function with the object is done the run time. Hence called run time polymorphism.
- Basically, the binding of function to object is done late, which is after compilation (i. e., during run time); hence, it is also named “Late binding”.
- Dynamic dispatch is a type of polymorphism or method dispatch which tells how java will select which functionality of the method will be used in run time. So the name is also known as Dynamic method Dispatch.
Conclusion
This concludes our learning of the topic “Overloading and Overriding in Java”. Write the codes mentioned in the above examples in the java compiler and check the output. Learning of codes will be incomplete if you will not do hands-on by yourself, enhancing your coding skills. Happy coding!!
Recommended Articles
This is a guide to the Overloading and Overriding in Java. Here we discuss methods in Overloading along with rules and limitations of Overriding in Java. You can also go through our other suggested articles to learn more –