Show
Method overloading in java is a feature that allows a class to have more than one method with the same name, but with different parameters. Java supports method overloading through two mechanisms:
What is Method overloading in Java?“Method overloading is a feature of Java in which a class has more than one method of the same name and their parameters are different.” In other words, we can say that Method overloading is a concept of Java in which we can create multiple methods of the same name in the same class, and all methods work in different ways. When more than one method of the same name is created in a Class, this type of method is called the Overloaded Method. We can easily understand about method of overloading by the below example: Suppose we have to write a method to find the square of an integer number. We can write this method as follows:
Suppose we want to find the square of 10, then to find the square of 10, we can call this method as follows: intSquare(5); Now, if we want to find the Square of a double type value, then we have to create another Square () method as follows:
Similarly, if we want to find the square of long type value, then we have to create another method as follows:
If we look carefully, to find the square of a number only, according to the data type of the number, we have to take three names as follows: intSquare() doubleSquare() longSquare() If it is possible that a programmer has to take only one name and the program itself decides which method to use for which type of value, then it will be easier for the programmer to get the same. There is no need to memorise the names of more than one method for type work. In Java, we can give the above three methods the same name. If we provide only the square () name instead of giving different names to the above three methods and write the rest of the description as follows, then Java’s Compiler does not generate any error.
If we define these three methods in a class, then these methods can be called Overloaded Methods as they have the same name. Let us develop CalculateSquare Class to understand this, which is as follows:
Note: We have not provided any argument in the ‘parenthesis’ of the square() method in our program. In this case, the Compiler Class calls the method in which no Parameter has been defined to achieve the Argument. Output: No Parameter Method Called Method with Integer Argument Called: 25 Method with float Argument Called: 6.25 In this way, we can define more than one Methods of the same name in a class, which is called Method Overloading, and The Java compiler itself performs the appropriate Method Call for an object, based on the Data Type of the Arguments of the Methods. Benefits of using Method Overloading
How to do Method Overloading?In java, we do method overloading in two ways:
In the example below, we have two methods, the first method has two arguments, and the second method has three arguments.
Output:- Result is 40 Result is 48.
In the example given below, we have two methods, and their data types are different.
Note: In this example, we are creating static methods so that we don’t need to create an instance for calling methods. Output: 30, 21 Some points to remember about method overloading:
Method overloading has nothing to do with return-type. If there are two methods of the same signature within a class in the program, then Ambiguity Error comes, whether their return-type is different or not. This means that method overloading has no relation with return-type.
Output: Sample.java:6: error: method disp(int) is already defined in class Sample In this way, we can define more than one Methods of the same name in a class called Method Overloading. The Java Compiler itself, based on the Data Type of the Arguments of the Methods, performs the appropriate method call for an object. This brings us to the end of the blog on Method Overloading in Java. Hope this helps you to up-skill your C++ skills. To learn more about programming and other related concepts, check out the courses on Great Learning Academy. Also, if you are preparing for Interviews, check out these Interview Questions for Java to ace it like a pro. Also Read: How to make Resume for Java Developer What is the process of defining two or more methods within same class that have same name but different parameters declaration in Java?If a class has multiple methods having same name but parameters of the method should be different is known as Method Overloading.
What is the process of defining to a more methods within same class that have same name but different parameter declaration?1. What is the process of defining two or more methods within same class that have same name but different parameters declaration? Explanation: Two or more methods can have same name as long as their parameters declaration is different, the methods are said to be overloaded and process is called method overloading.
Which of the following is a method having same name as that of its class?A constructor is a method that initializes an object immediately upon creation. It has the same name as that of class in which it resides.
What is the process of defining a method in a subclass having same?If subclass (child class) has the same method as declared in the parent class, it is known as method overriding in Java.
|