• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

overriding and overloading

 
Shumani Dolresia Edzani
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the differences between overriding and overloading
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In a few words:

You override a method when you replace the implementation of an inherited method from a base class in a subclass of it. The overridden method in the subclass has to have the same method signature (name, parameters, modifiers) than the method inherited from the base class.

Overloading a method means that you create a method with the same name as another method in the same class but with different type or number of parameters.

For more information take a look at Sun's Java tutorial!

Marco
[ March 28, 2008: Message edited by: Marco Ehrentreich ]
 
Shafi mia
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you overload you MUST change the argument list. But in override the arguments must remain same or subtypes..
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did I say something else?
 
Sandeep Bhandari
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
follow this for overloading rules:

overloading rules
 
Niala Nirell
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shafi mia:
If you overload you MUST change the argument list. But in override the arguments must remain same or subtypes..


the value of return can be the same or subtype (covariance) but the argument list must remain the same. Am I wrong?
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Java 1.5 introduced covariant return types! But I'm quite sure you are only allowed to use them for overridden methods not overloaded methods.

For explanation to Shumani: Covariant return types mean that when you override a method in a subclass you are allowed to change the return type to a derived class of the return type used by the superclass method.

And to cause some more confusion there are more subtleties regarding overriding methods. You are even allowed to widen the access modifier of a method. So you can override a protected method from a superclass with a public method in the subclass. But you're just allowed to widen access modifiers not to narrow them because otherwise the public API of a class could become invalid in subclasses.

[ March 28, 2008: Message edited by: Marco Ehrentreich ]
[ March 28, 2008: Message edited by: Marco Ehrentreich ]
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's one more thing I've forgotten regarding overridden methods...

Overridden methods are allowed to throw any additional RUNTIME exceptions the base class method doesn't throw.

And they are allowed to throw less checked exceptions or subtypes of the checked exception(s) the overridden method in the superclass declares in its throws clause. But you are not allowed to override it with a broader or totally different type of checked exception.
 
Snigda Kakarla
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Overloaded methods have different argument list,can have different return types,can have different acess modifiers and can declare border checked exceptions.
Overridden methods must have same argument list,same return type,access level should not be more restrictive.These methods can be inherited.one cannot override a method if it is marked final or static.
 
Sandeep Bhandari
Ranch Hand
Posts: 201
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would like to say that, its the method calls which determine the ambiguity of overloaded methods.

e.g.
void foo(long l,float t) {}
void foo(int t,float f) {}

foo(2.0,2.4f);

makes the compiler to flash error.
 
Matthew Walton
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Sandeep Bhandari:
I would like to say that, its the method calls which determine the ambiguity of overloaded methods.

e.g.
void foo(long l,float t) {}
void foo(int t,float f) {}

foo(2.0,2.4f);

makes the compiler to flash error.


Yes, because 2.0 is a double constant, and the compiler doesn't have a preference whether it should convert to long or to int, so it can't make a choice between the two.

I believe if you replace the 2.0 in the foo call with 2, it'll be an int constant and will thus call the int, float overload.
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a small overview which types are implicitly casted by Java to another type. As you see implicit casts are only possible up the hierarchy. Otherwise you would there could be a potential loss in precision so you have to force the compiler to explicitly cast a type down the hierarchy (not so good idea unless absolutely necessary).

If you use numeric literals like "2.0" Java automatically chooses an adequate type for it internally which is double in this case. In this case the compiler simply doesn't know that 2.0 could be cast to an int without loss of precision. That are the rules!

Marco
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a good link Marco Ehrentreich
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sandeep Bhandari, the url what you have shown also seem to be explaining the concept nicely and in detail.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic