Popular Branches
MBA
B.Tech
BBA
BSc
Updated on 03rd July, 2024 , 3 min read
The Java programming language paraded onto the scene fully embracing the concept of the OOP-(Object-Oriented Programming).In the Object-Oriented Programming, one of the groovy moves is Polymorphism. It is a dedicated way of saying that objects and methods in your program can hoof in different ways depending on the circumstances. In easier words we say that , a method with the same name can do different actions and can pull out various results, all based on the parameters.
In Java, the Method Overloading allows the nonidentical methods to have the same name, but dissimilar signatures where the signature can vary by the no. of input arguments, type of input arguments, or a mixture of both. Let's assume that a class has many methods of having the same name but vary in parameters, it is called Method Overloading and if we have to carry on only one operation, then having the same name of the methods enhance the readability and reusability of the program.
In Java, besides method overloading, there is another method similar to it which is called method overriding. The latter is also connected to polymorphism, but they serve various purposes and have dissimilar rules, which is also related to another important OOP-(Object-Oriented Programming) concept - inheritance.
So method overriding is a feature that enhances a subclass to provide a specific implementation for a method that is already in its parent class. This is a basic part of the inheritance concept in OOP -(object-oriented programming), allowing the runtime polymorphism.
Here is a small code example. Let's create the class 'Musical Instrument' and two subclasses: 'Piano' and 'Violin'. Both the instruments are meant for playing, but playing a different technique for each. One will override the parent method play() in both subclasses to illustrate the concept.
In Java, Method Overloading allows various methods to have the same name, but vary signatures where these signatures can differ by the no. of input parameters and types of input arguments, and a mixture of both.
In Method overloading in Java is also called Compile-time Polymorphism, static polymorphism, and early binding. In Method overloading compared to the parent argument, the child argument also gets the highest priority.
Also see: Collection framework in java
There are three different ways of overloading in Java:
The following are the advantages of method overloading:
Yes, by method overloading one can overload the java main()method. One can have whichever number of main methods in a class by the method overloading. But JVM calls the main() method which gets the string array only as arguments.
Also see: Abstract keyword in Java
Pilot Salary in India 2024: Starting Salary, Requirements, Qualifications, Per Month Salary
By - Nikita Parmar 2024-09-06 10:59:22 , 6 min readThe Java programming language paraded onto the scene fully embracing the concept of the OOP-(Object-Oriented Programming).In the Object-Oriented Programming one of the groovy moves is Polymorphism.
In Java, Method Overloading allows various methods to have the same name, but vary signatures where these signatures can differ by the no. of input parameters and types of input arguments,and a mixture of both.
Method overloading improvises the readability and reusability of the programs. Method overloading minimizes the complexity of the programs. Using method overloading, programmers can perform a task more efficiently and more effectively.
Yes, by method overloading one can overload java main()method. One can have whichever number of main methods in a class by the method overloading.