answersLogoWhite

0

When will overloading happens?

User Avatar

Anonymous

14y ago
Updated: 8/17/2019

Overloaded methods let you reuse the same method name in a class, but with different arguments (and optionally, a different return type). Overloading a method often means you're being a little nicer to those who call your methods, because your code takes on the burden of coping with different argument types rather than forcing the caller to do conversions prior to invoking your method. The rules are simple:

• Overloaded methods MUST change the argument list.

• Overloaded methods CAN change the return type.

• Overloaded methods CAN change the access modifier.

• Overloaded methods CAN declare new or broader checked exceptions.

• A method can be overloaded in the same class or in a subclass. In other words, if class A defines a doStuff(int i) method, the subclass B could define a doStuff(String s) method without overriding the superclass version that takes an int. So two methods with the same name but in different classes can still be considered overloaded, if the subclass inherits one version of the method and then declares another overloaded version in its class definition.

Let us say we have the Ferrari class with a method drive() as below:

Public class Ferrari extends Car {

Public void drive() {…}

}

Below are the legal overload method declarations:

1. public void drive(String destination, String roadToTake) {…}

2. public void drive(String destination, int timeToTake) {…}

3. public void drive(String destination) throws RouteNotFoundException {…}

Assuming that the Ferrari class has the above 3 methods, below are is an illegal overload:

1. public String drive (String destination, String roadToTake) {…} - You cannot have two methods which differ just in the return type. This is not allowed

User Avatar

Wiki User

14y ago

What else can I help you with?

Related Questions

What happens when the ant takes flight?

Flight cannot take off because of overloading...


Operator overloading is possible in java or not?

Java does not support opperator overloading, so the answer to your question is: none.


What is the scope of method overloading and overriding?

Overloading happens when you have multiple methods in the current class that have the same name but different signature. The scope of method overloading is "Within the current class" Overriding happens when your current class extends another class (the parent class) and provides implementation for a method that is already available in the parent class. The scope of method overriding too is "Within the current class"


Is it possible to do operator overloading in c?

No. Operator and/or function overloading is only a C++ thing.


Why you use constructor overloading?

When we are initializing our object with different internal state then we can use the constructor overloading.


Why is overloading a watercraft considered reckless and negligent behaviour?

Overloading a watercraft could cause a capsize and the vessel to sink.


Is constructor overloading same as method overloading.yes or no. explain?

yes,because in constructor overloading constructor have same and different parameter list. In method overloading method have same name and different parameter list.


define function overloading?

Defining several functions with the same name with unique list of parameters is called as function overloading.


Why is overloading an outlet dangerous?

Overloading an outlet can cause the circuit to overheat, increasing the risk of electrical fires. It can also damage the outlet itself, as well as any connected appliances or devices. It is important to evenly distribute the load among multiple outlets to avoid overloading.


What happens at the rated value of the voltage of the generator?

The rated voltage of a generator decreases due to many causes such as armature reaction, overloading of the generator and AVR failure/ weak excitation voltage.


C coding for operator overloading?

C does not support operator overloading. If you mean C++ operator overloading, it depends on exactly what you wanted to do. If you wanted to '+' to strings, then you could write: string operator+(string a, string b) { // do something }


Is overloading supported in csharp?

Yes