0

I was just trying to understand the polymorphism and the classic discussion of method overloading and method overriding came ,what confused me is there performance issue . So which is a better thing if the situation is like i can go with either of the two concepts. I googled but dint get anything fruitful.

I understand that they are completely two diff concepts and have their own importance and use ,but in case if there is a chance to choose one which one to prefer method overriding or method overloading ?

Sham
  • 414
  • 2
  • 6
  • 14
  • 6
    The answer is within your own question. `completely two diff concepts` they are not alternative of each other so there is no chance of choosing any one. [learn more...](http://stackoverflow.com/questions/12893907/is-polymorphism-overloading-and-overriding-are-same-concepts) – Braj Jul 10 '14 at 18:51
  • Do you have an example of a set of classes where you find yourself choosing between method overloading and overriding? – Austin Mullins Jul 10 '14 at 18:54
  • yes they are not alternative of each other ,but am just wondering from academics point of view,given a case when we can solve a situation by both of the two concepts which one to prefer then ? – Sham Jul 10 '14 at 18:54
  • only from understanding point of view i can either overload or override the area method to find the area of 3 geometrical shapes. – Sham Jul 10 '14 at 18:57
  • @Sham Overloading gives better performance than overriding. – ritesh Jul 10 '14 at 18:57
  • @Ritesh not in Java, there is no difference. – Peter Lawrey Jul 10 '14 at 18:58
  • @ Ritesh how throw some light to your answer – Sham Jul 10 '14 at 18:58

1 Answers1

0

choose one which one to prefer method overriding or method overloading ?

Most performance questions should actually be treated as; What is the simplest and clearest way to implement this code.

In this case, it doesn't have anything to do with performance. Just stick with the simplest and clearest option.

from academics point of view,given a case when we can solve a situation by both of the two concepts which one to prefer then ?

You would need to give an example, because I can't think of one.

to find the area of 3 geometrical shapes.

I would override the Shape.area() method, and in Java this would be simpler, and just as fast as three overloaded methods, depending on how you implemented it.

Say you did

Triangle t = 
Circle c = 
Square s = 
double area = t.area() + c.area() + s.area();

This would be just as fast as

double area = t.area("String") + c.area(123L) + s.area(MyEnum.VALUE);

Assuming the arguments are just there to force different overloaded methods to be called, but as you can see this is not simple or clear why you would want to do this.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • @ Peter i have mentioned a situation where i can either overload or override an area() to calculate areas of different geometries. – Sham Jul 10 '14 at 19:00
  • @Sham It entirely depends on how you call those methods, they are not interchangable. – Peter Lawrey Jul 10 '14 at 19:01
  • yes i am getting your point they are not interchangeable ,but why you chose to override?that is what am wondering – Sham Jul 10 '14 at 19:04