answersLogoWhite

0


Best Answer

The only disadvantage of operator overloading is when it is used non-intuitively. All operators must behave with predictable results, thus it makes no sense to implement the plus (+) operator so that it behaves like a subtract (-) operator, or a multiply (*) operator, or indeed anything other than the intuitive sum of two objects.

User Avatar

Elijah Koch

Lvl 10
2y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

14y ago

Overloading in Java is the ability to create multiple methods of the same name, but with different parameters. The main advantage of this is cleanliness of code. Let's take the String.valueOf method. The overloaded versions of this method are defined as:

static String valueOf(boolean b)

static String valueOf(char c)

static String valueOf(char[] data)

static String valueOf(char[] data, int offset, int count)

static String valueOf(double d)

static String valueOf(float f)

static String valueOf(int i)

static String valueOf(long l)

static String valueOf(Object obj)

This means that if we have any type of variable, we can get a String representation of it by using String.valueOf(variable). If overloading was not allowed we'd have methods that look like this...

static String valueOfBoolean(boolean b)

static String valueOfChar(char c)

static String valueOfCharArray(char[] data)

static String valueOfCharArrayWithOffset(char[] data, int offset, int count)

static String valueOfDouble(double d)

static String valueOfFloat(float f)

static String valueOfInt(int i)

static String valueOfLong(long l)

static String valueOfObject(Object obj)

...which is very ugly and harder to read than the overloaded solution.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago
The advantage of operator overloading is that consumers can use your objects in an intuitive manner. For instance, it is clearly more intuitive to say x=42 than it would be to call a mutator member such as x.set(5). By the same token, if your class provides a sequential container then it is intuitive to be able to access the elements of that container using the subscript operator rather than calling a getter member.

The disadvantage of operator overloading is only when they are used inappropriately. If such a usage cannot be regarded as being intuitive or transparent to consumers, then it would be better to just not provide the operator at all (use member methods instead). For instance, if x=42 makes no logical sense with respect to your object, then it makes no sense to provide an operator=(const int) assignment operator overload in your object's class. And if your object does not represent a sequental container, then it makes no sense to provide an operator[](const size_t) subscript operator overload.
This answer is:
User Avatar

User Avatar

Wiki User

12y ago

It's very handy when you create a class which does not follow, for instance, usual math operations. Overloading allows you to adopt operations on your class for your particular purposes.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

overloading means same function but different arguments. so with overloading we can take different actions with different parameters....

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the importance of overloading and overriding?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

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"


What is method overloading and constructor overloading explain with?

method overriding :method overriding means redefine methods in sub classes they already defined in the Super classes.method overloading : It means methods with the same name but with a different signature exist in one class


What is the difference between overloading and overriding methods in object?

Here are some of the most common differences between both of them. If you are working in Java for more than 1 year, you might be familiar with all of them but any way its good revision: 1) First and major difference between Overloading and Overriding is that former occur during compile time while later occur during runtime. 2) Second difference between Overloading and Overriding is that, you can overload method in same class but you can only override method in sub class. 3) Third difference is that you can overload static method in Java but you can not override static method in Java. In fact when you declare same method in Sub Class it's known as method hiding because it hide super class method instead of overriding it. 4) Overloaded methods are bonded using static binding and Type of reference variable is used, while Overridden method are bonded using dynamic bonding based upon actual Object. 5) Rules of Overloading and Overriding is different in Java. In order to overload a method you need to change its method signature but that is not required for overriding any method in Java.


Difference between Method overloading and method overriding in C plus plus?

Yes. Any base class method that is declared virtual can be overridden by a derived class. Overriding a method that is not declared virtual can still be called, but will not be called polymorphically. That is, if you call the base class method, the base class method will execute, not the override. To call a non-virtual override you must call it explicitly.


Why do you use of operator overloading in c?

Overloading, Overriding, Polymorphism, Information Hiding, Inheritance all these are CONCEPTS of C++ and Java. An Object Oriented Language and not of C language. Thats why Bjarne Stroustrup came up with C++ ...

Related questions

Is it possible to do method overloading and overriding at a time?

no we cannot do both at the same time because overloading requires different arguments and overriding dosenot


What is method overriding and overloading in java?

Overloading is the means by which we can provide two or more different definitions of the same method in the same namespace. Overriding is the means by which a derived class may redefine the meaning of a base class method.


What would happen if atrial contraction occurred late?

There will be an overriding of the atria or overloading of the heart.


What is a operator overriding in java?

Java does not support object overriding. It does support operator overloading by means of the "+" symbol which is used for both numeric addition as well as string concatenation.


How you compare and contrast overloading and overriding methods in java?

Method overloading is when you have multiple methods in a class that have the same name but a different signature. Method overriding is similar to method overloading, with a small difference. In overriding, a method in a parent class is overridden in the child class. The method in the child class will have the same signature as that of the parent class. Since the method in the child class has the same signature & name as the method of its parent class, it is termed as overriding. In situations where you may have to explicitly call the parent class method you can use the "super" keyword and for explicitly calling the current objects method you can use the "this" keyword.


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"


What is the difference between compile time and run time polymorphism?

Runtime prolymorphism means overriding compiletile polymorphism means overloading


What is differnce between overloading and overriding?

Overloading is when you're asking something, usually some sort of machinery, to do more than it's designed to. Say you have an elevator that's rated for 5 people, but you cram more persons into it - then the elevator is overloaded. Overriding is when an operator decides to ignore or bypass a warning. Say that the warning light for low oil pressure/level lights up in a car, but the driver decides to keep going - then the driver is overriding the warning signal.


What is the difference between over loading and over riding?

overloading” is having the functions (methods) with the same name but different signatures. Overloading acts on different data types in different ways.“overriding” is having a methods with same name and same signature in a parent class and the child class. Overriding acts on different object types in different ways. ...... ambuj24@gmail.com


What is method overloading and constructor overloading explain with?

method overriding :method overriding means redefine methods in sub classes they already defined in the Super classes.method overloading : It means methods with the same name but with a different signature exist in one class


What is the opposite of overriding?

The opposite of the gerund overriding would be approving or endorsing.(In the US Congress, the failure to override a veto is confirming it.)The adjective overriding (meaning of primary importance, overarching) has the opposites secondary, insignificant, negligible, or unimportant.