PROGRAMMING, LOGIC and DESIGN 8th EDITION
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Go down
avatar
Admin
Admin
Posts : 11
Join date : 2018-08-07
Age : 25
https://rachellann.forumotion.com

Overloading Methods Empty Overloading Methods

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.
Overloading Methods Screen24

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 Screen25

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.

Overloading Methods Screen26

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?
avatar
Hyegi
Guru
Guru
Posts : 10
Join date : 2018-08-14

Overloading Methods Empty Re: Overloading Methods

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

Overloading Methods Empty Re: Overloading Methods

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

Overloading Methods Empty Re: Overloading Methods

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?
avatar
Hyegi
Guru
Guru
Posts : 10
Join date : 2018-08-14

Overloading Methods Empty Re: Overloading Methods

Tue Aug 14, 2018 8:42 am
Can overloaded methods be synchronized?
avatar
Hyegi
Guru
Guru
Posts : 10
Join date : 2018-08-14

Overloading Methods Empty Re: Overloading Methods

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

Overloading Methods Empty Re: Overloading Methods

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
avatar
nicolai
Guru
Guru
Posts : 10
Join date : 2018-08-14

Overloading Methods Empty Re: Overloading Methods

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.
avatar
nicolai
Guru
Guru
Posts : 10
Join date : 2018-08-14

Overloading Methods Empty Re: Overloading Methods

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.
avatar
nicolai
Guru
Guru
Posts : 10
Join date : 2018-08-14

Overloading Methods Empty Re: Overloading Methods

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

Yes. Overloaded methods can be synchronized.
avatar
nicolai
Guru
Guru
Posts : 10
Join date : 2018-08-14

Overloading Methods Empty Re: Overloading Methods

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

Yes, we can declare overloaded methods as final.
avatar
wheein
Posts : 2
Join date : 2018-08-17

Overloading Methods Empty Re: Overloading Methods

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
avatar
wheein
Posts : 2
Join date : 2018-08-17

Overloading Methods Empty Re: Overloading Methods

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

Overloading Methods Empty Re: Overloading Methods

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(){ }
}
avatar
ekkoekko
Guru
Guru
Posts : 10
Join date : 2018-08-18

Overloading Methods Empty Re: Overloading Methods

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

Overloading Methods Empty Re: Overloading Methods

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.
avatar
ekkoekko
Guru
Guru
Posts : 10
Join date : 2018-08-18

Overloading Methods Empty Re: Overloading Methods

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?
avatar
janna
Guru
Guru
Posts : 10
Join date : 2018-08-18

Overloading Methods Empty Re: Overloading Methods

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
}
avatar
janna
Guru
Guru
Posts : 10
Join date : 2018-08-18

Overloading Methods Empty Re: Overloading Methods

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

Overloading Methods Empty Re: Overloading Methods

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

Overloading Methods Empty Re: Overloading Methods

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

Overloading Methods Empty Re: Overloading Methods

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.
avatar
kathrynmoto
Guru
Guru
Posts : 10
Join date : 2018-08-17

Overloading Methods Empty Re: Overloading Methods

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
avatar
kathrynmoto
Guru
Guru
Posts : 10
Join date : 2018-08-17

Overloading Methods Empty Re: Overloading Methods

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
avatar
annmaganda
Guru
Guru
Posts : 10
Join date : 2018-08-20

Overloading Methods Empty Re: Overloading Methods

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

Overloading Methods Empty Re: Overloading Methods

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