Java/J2EE and SOA (349 Blogs) Become a Certified Professional

Methods and Method Overloading in Java

Last updated on Oct 25,2020 7K Views


Method Overloading in Java is a concept related to Object Oriented Programming (OOP). Java supports overloading of methods and can distinguish between different methods with method signatures. A situation, wherein, in the same class there are two or more methods with same name, having different functions or different parameters, it is called Method Overloading. To take an in depth look at what Method Overloading is to have a better understanding of this concept.

Methods

A method is a collection of statements that are grouped together to perform an operation. It takes some parameters, performs some computations, and then optionally returns a value (or object). To create a particular functionality, a method is needed to be created so as to modularize the application. By making a method, one can call that particular method at different instances. In this way, the code redundancy can be reduced and the code can be reused. There are five components in a method:

  1. Modifiers– There can be four types of modifiers – Public, Private, Protected, and Default (uncategorized) modifiers.
  2. The return type– A method can return a value, which could be any one of integer, string, character, float or nothing. In case, a method is not returning anything, ‘void’ is needed to be mentioned in the place of return type, which will show that the method will not return anything. A method, which is not returning anything, or has a void return type, is known as procedure. If a method is returning a value, it is termed as function.
  3. The method name– It’s the identifier of a method. Using the name of the method, one can use that particular method.
  4. The parameter list in parenthesis– It is the list of arguments. If a method comprises of certain values, which it needs to compare, e.g. for performing addition of two numbers, a method will require two number as parameters. In this case, you need to pass int a, int b as the parameters.
  5. The method body enclosed with braces– In body, you need to provide the definition of the operation that has to be performed. The functionality of the method is presented in the body, enclosed in curly braces.

Method Overloading

Now, that we have an idea of what a method is, let’s go back to Method Overloading that we defined in the beginning of this post – It is a situation, wherein, in the same class there are two or more methods with same name, having different functions or different parameters. Following are the conditions for method overloading in Java:

  • The overloaded function must differ either by the number of arguments or operands or data types.
  • The same function name is used for various instances of function call.

Method Overloading can be done in two ways:

  1. By changing Arguments’ data types
  2. By changing number of Arguments

Why Method Overloading?

Using Method Overloading in Java is very common among Java programmers, because it:

  • Provides flexibility to call similar method for different data types
  • Saves memory
  • Saves time
  • Increases consistency
  • Improves code readability.

Got a question for us? Mention them in the comments section and we will get back to you. 

Related Posts:

Unveil the Magic of Java with Edureka!

Hadoop for Java Professionals

Learn Java/J2EE & SOA

 

Java

Upcoming Batches For Java Course Online
Course NameDateDetails
Java Course Online

Class Starts on 7th December,2024

7th December

SAT&SUN (Weekend Batch)
View Details
Comments
1 Comment

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

Methods and Method Overloading in Java

edureka.co