Share
Go down
Admin
Posts : 11
Join date : 2018-08-07
Age : 19
View user profilehttp://rachellann.forumotion.com

Overloading Methods

on Wed Aug 08, 2018 2:55 pm
In programming, overloading involves supplying diverse meanings for a single identifier. When you use the English language, you frequently overload words. When you say break a window, break bread, break the bank, and take a break, you describe four very different actions that use different methods and produce different results.


When you overload a method , you write multiple methods with a shared name but different parameter lists. When you call an overloaded method, the language translator understands which version of the method to use based on the arguments used. For example, suppose that you create a method to output a message and the amount due on a customer bill, as shown in Figure 9-17. The method receives a numeric parameter that represents the customer’s balance and produces two lines of output. Assume that you also need a method that is similar to printBill() , except the new method applies a discount to the customer bill.

One solution to this problem would be to write a new method with a different name—for example, printBillWithDiscount() . A downside to this approach is that a programmer who uses your methods must remember the names of each slightly different version. It is more natural for your methods’ clients to use a single well-designed method name for the task of printing bills, but to be able to provide different arguments as appropriate. In this case, you can overload the printBill() method so that, in addition to the version that takes a single numeric argument, you can create a version that takes two numeric arguments—one that represents the balance and one that represents the discount rate. Figure 9-17 shows the two versions of the printBill() method.


If both versions of printBill() are included in a program and you call the method using a single numeric argument, as in printBill(custBalance) , the first version of the method in Figure 9-17 executes. If you use two numeric arguments in the call, as in printBill (custBalance, rate) , the second version of the method executes.


The second version of the method in Figure 9-18 accepts three parameters, providing a balance, discount rate, and customized message. For example, the following method call would use this version of the method:
printBill(balanceDue, discountRate, specialMessage)




Overloading methods is never required in a program. Instead, you could create multiple methods with unique identifiers such as printBill() and printBillWithDiscountAndMessage() .
Overloading methods does not reduce your work when creating a program; you need to write each method individually. The advantage is provided to your method’s clients; those who use your methods need to remember just one appropriate name for all related tasks.

Avoiding Ambiguous Methods


When you overload a method, you run the risk of creating ambiguous methods —a situation in which the compiler cannot determine which method to use. Every time you call a method, the compiler decides whether a suitable method exists; if so, the method executes, and if not, you receive an error message.

For example, suppose that you write two versions of a printBill() method, as shown in the program in Figure 9-19. One version of the method is intended to accept a customer balance and a discount rate, and the other is intended to accept a customer balance and a discount amount expressed in dollars.



Each of the two versions of printBill() in Figure 9-19 is a valid method on its own.

However, when the two versions exist in the same program, a problem arises. When the main program calls printBill() using two numeric arguments, the compiler cannot determine which version to call. You might think that the version of the method with a parameter named discountInDollars would execute, because the method call uses the identifier discountInDollars .

However, the compiler determines which version of a method to call based on argument data types only, not their identifiers. Because both versions of the
printBill() method could accept two numeric parameters, the compiler cannot determine which version to execute, so an error occurs and program compilation stops.
An overloaded method is not ambiguous on its own—it becomes ambiguous only if you make a method call that matches multiple method signatures. In many languages, a program with potentially ambiguous methods will run without problems if you don’t make any method calls that match more
than one method.

Methods can be overloaded correctly by providing different parameter lists for methods with the same name. Methods with identical names that have identical parameter lists but different return types are not overloaded—they are ambiguous. For example, the following two method headers create ambiguity:
string aMethod(num x)
num aMethod(num y)


The compiler determines which version of a method to call based on parameter lists, not return types. When the method call aMethod(17) is made, the compiler will not know which of the two methods to execute because both possible choices take a numeric argument.

TWO TRUTHS & A LIE

[justify]Overloading Methods
1. In programming, overloading involves supplying diverse meanings for a single identifier.
2. When you overload a method, you write multiple methods with different names but identical parameter lists.
3. Methods can be overloaded correctly by providing different parameter lists for methods with the same name.

WHICH OF THEM IS A LIE?
Guru
Guru
Posts : 10
Join date : 2018-08-14
View user profile

Re: Overloading Methods

on Tue Aug 14, 2018 8:40 am
What is method signature? What are the things it consist of?
Guru
Guru
Posts : 10
Join date : 2018-08-14
View user profile

Re: Overloading Methods

on Tue Aug 14, 2018 8:41 am
How do compiler differentiate overloaded methods from duplicate methods?
Guru
Guru
Posts : 10
Join date : 2018-08-14
View user profile

Re: Overloading Methods

on Tue Aug 14, 2018 8:41 am
Is it possible to have two methods in a class with same method signature but different return types?
Guru
Guru
Posts : 10
Join date : 2018-08-14
View user profile

Re: Overloading Methods

on Tue Aug 14, 2018 8:42 am
Can overloaded methods be synchronized?
Guru
Guru
Posts : 10
Join date : 2018-08-14
View user profile

Re: Overloading Methods

on Tue Aug 14, 2018 8:42 am
Can we declare overloaded methods as final?
Guru
Guru
Posts : 10
Join date : 2018-08-14
View user profile

Re: Overloading Methods

on Tue Aug 14, 2018 8:48 am
Hyegi wrote:What is method signature? What are the things it consist of?
Method signature is used by the compiler to differentiate the methods. Method signature consist of three things.

a) Method name

b) Number of arguments

c) Types of arguments
Guru
Guru
Posts : 10
Join date : 2018-08-14
View user profile

Re: Overloading Methods

on Tue Aug 14, 2018 8:48 am
Hyegi wrote:How do compiler differentiate overloaded methods from duplicate methods?

Compiler uses method signature to check whether the method is overloaded or duplicated. Duplicate methods will have same method signatures i.e same name, same number of arguments and same types of arguments. Overloaded methods will also have same name but differ in number of arguments or else types of arguments.
Guru
Guru
Posts : 10
Join date : 2018-08-14
View user profile

Re: Overloading Methods

on Tue Aug 14, 2018 8:49 am
Hyegi wrote: Is it possible to have two methods in a class with same method signature but different return types?

No, compiler will give duplicate method error. Compiler checks only method signature for duplication not the return types. If two methods have same method signature, straight away it gives compile time error.
Guru
Guru
Posts : 10
Join date : 2018-08-14
View user profile

Re: Overloading Methods

on Tue Aug 14, 2018 8:50 am
Hyegi wrote:Can overloaded methods be synchronized?

Yes. Overloaded methods can be synchronized.
Guru
Guru
Posts : 10
Join date : 2018-08-14
View user profile

Re: Overloading Methods

on Tue Aug 14, 2018 8:51 am
Hyegi wrote:Can we declare overloaded methods as final?

Yes, we can declare overloaded methods as final.
Posts : 2
Join date : 2018-08-17
View user profile

Re: Overloading Methods

on Fri Aug 17, 2018 8:47 am
Hyegi wrote:Can we declare overloaded methods as final?


Last edited by wheein on Fri Aug 17, 2018 8:53 am; edited 1 time in total
Posts : 2
Join date : 2018-08-17
View user profile

Re: Overloading Methods

on Fri Aug 17, 2018 8:53 am
Difference between method overloading and overriding?
Guru
Guru
Posts : 10
Join date : 2018-08-18
View user profile

Re: Overloading Methods

on Sat Aug 18, 2018 6:40 pm
Hyegi wrote:Can overloaded methods be synchronized?

I think. Yes it can. Synchronized, native, and strictfp are keyword modifiers that specify an implementation detail. You can declare a base class method without a synchronized keyword, but add it to the overridden version. You're not changing the API at all, you're just changing the implementation, and that's what overriden methods are good at!
here's an example:

public class TestOverrideKeywords{

void baseMethod1(){ }

public static void main(String[] args) {
Child c = new Child();

}

}
class Child extends TestOverrideKeywords{

synchronized void baseMethod1(){ }
}
Guru
Guru
Posts : 10
Join date : 2018-08-18
View user profile

Re: Overloading Methods

on Sat Aug 18, 2018 6:44 pm
What Does Method Signature Mean in Java?
Guru
Guru
Posts : 10
Join date : 2018-08-18
View user profile

Re: Overloading Methods

on Sat Aug 18, 2018 6:45 pm
ekkoekko wrote:What Does Method Signature Mean in Java?

a method signature is part of the method declaration in java. It's the combination of the method name and the parameter list.
Guru
Guru
Posts : 10
Join date : 2018-08-18
View user profile

Re: Overloading Methods

on Sat Aug 18, 2018 6:47 pm
janna wrote:
ekkoekko wrote:What Does Method Signature Mean in Java?

a method signature is part of the method declaration in java. It's the combination of the method name and the parameter list.

can you give an example?
Guru
Guru
Posts : 10
Join date : 2018-08-18
View user profile

Re: Overloading Methods

on Sat Aug 18, 2018 6:49 pm
ekkoekko wrote:
janna wrote:
ekkoekko wrote:What Does Method Signature Mean in Java?

a method signature is part of the method declaration in java. It's the combination of the method name and the parameter list.

can you give an example?
public void setMapReference(Point position)
{
//method code
}
Guru
Guru
Posts : 10
Join date : 2018-08-18
View user profile

Re: Overloading Methods

on Sat Aug 18, 2018 7:20 pm
What is the difference between method overloading and method overriding in Java?
Guru
Guru
Posts : 10
Join date : 2018-08-18
View user profile

Re: Overloading Methods

on Sat Aug 18, 2018 7:20 pm
Can we overload Java main () method?
Guru
Guru
Posts : 10
Join date : 2018-08-18
View user profile

Re: Overloading Methods

on Sat Aug 18, 2018 7:21 pm
How many types of methods are there in Java?
Guru
Guru
Posts : 10
Join date : 2018-08-17
View user profile

Re: Overloading Methods

on Mon Aug 20, 2018 1:57 pm
janna wrote:How many types of methods are there in Java?

Java has three different types of methods.
1. Static methods: A static method is a method that can be called and executed without creating an object.
2. Instance methods: These methods act upon the instance variables of a class.
3. Factory methods: A factory method is a method that returns an object to the class to which it belongs. All factory methods are static methods.
Guru
Guru
Posts : 10
Join date : 2018-08-17
View user profile

Re: Overloading Methods

on Mon Aug 20, 2018 1:59 pm
janna wrote:What is the difference between method overloading and method overriding in Java?

Overloading occurs when two or more methods in one class have the same method name but different parameters while Overriding means having two methods with the same method name and parameters
Guru
Guru
Posts : 10
Join date : 2018-08-17
View user profile

Re: Overloading Methods

on Mon Aug 20, 2018 2:01 pm
janna wrote:Can we overload Java main () method?

You can overload the main() method, but only public static void main
Guru
Guru
Posts : 10
Join date : 2018-08-20
View user profile

Re: Overloading Methods

on Mon Aug 20, 2018 3:20 pm
C# not only supports method overloading, but revels in it. Most of the methods you'll find built into C# have several overloaded forms to make life easier for you—for example, you can call System.Console.WriteLine with a string, a float, an int, and so on, all because it's been overloaded to handle those types of arguments (it has 18 overloaded forms in all).
Sponsored content

Re: Overloading Methods

Back to top
Permissions in this forum:
You cannot reply to topics in this forum