Method overloading is a feature in Java that allows multiple methods with the same name to be defined in a class. These methods can have different parameter lists, which enables them to perform different actions based on the arguments passed to them. In this article, we will discuss the different ways of method overloading in Java.
Overloading by changing the number of parameters: One way to overload a method is by changing the number of parameters it takes. For example, let's say we have a method called "add" that takes two integers as parameters. We can overload this method by creating another method with the same name, but this time it takes three integers as parameters.
Overloading by changing the data types of parameters: Another way to overload a method is by changing the data types of its parameters. For example, let's say we have a method called "print" that takes a string as a parameter. We can overload this method by creating another method with the same name, but this time it takes an integer as a parameter.
Overloading by changing the order of parameters: We can also overload a method by changing the order of its parameters. For example, let's say we have a method called "divide" that takes two integers as parameters. We can overload this method by creating another method with the same name, but this time it takes the second integer parameter first.
Overloading by changing the return type: Another way to overload a method is by changing its return type. However, this approach has a few limitations. The methods must have the same name, the same parameters, and the same access level. Additionally, the only thing that can be changed is the return type. For example, let's say we have a method called "add" that returns an integer. We can overload this method by creating another method with the same name, same parameters, but this time it returns a string.
Overloading by changing the access modifiers: We can also overload a method by changing its access modifiers. For example, let's say we have a method called "sum" that is public. We can overload this method by creating another method with the same name, but this time it is private.
Australia
UK
UAE
Singapore
Canada
New
Zealand
Malaysia
USA
India
South
Africa
Ireland
Saudi
Arab
Qatar
Kuwait
Hongkong
Copyright 2016-2023 www.programmingshark.com - All Rights Reserved.
Disclaimer : Any type of help and guidance service given by us is just for reference purpose. We never ask any of our clients to submit our solution guide as it is, anywhere.