File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes overriding and overloading Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "overriding and overloading" Watch "overriding and overloading" New topic
Author

overriding and overloading

Shumani Dolresia Edzani
Greenhorn

Joined: Mar 28, 2008
Posts: 4
what is the differences between overriding and overloading
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

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

Joined: Feb 18, 2008
Posts: 10
If you overload you MUST change the argument list. But in override the arguments must remain same or subtypes..


SCJP 5.0
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

Did I say something else?
Sandeep Bhandari
Ranch Hand

Joined: Apr 16, 2004
Posts: 201
follow this for overloading rules:

overloading rules


SCJP 96% | SCWCD 90%| SCJP mock exams | My SCJP blog
Niala Nirell
Ranch Hand

Joined: Mar 12, 2008
Posts: 46
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

Joined: Mar 07, 2007
Posts: 1280

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

Joined: Mar 07, 2007
Posts: 1280

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

Joined: Mar 29, 2008
Posts: 4
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

Joined: Apr 16, 2004
Posts: 201
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

Joined: Dec 18, 2007
Posts: 12
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.


What do you mean, 'only String gets to have overloaded operators'?
Marco Ehrentreich
best scout
Bartender

Joined: Mar 07, 2007
Posts: 1280

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

Joined: Apr 20, 2006
Posts: 3344

That's a good link Marco Ehrentreich


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Sandeep Bhandari, the url what you have shown also seem to be explaining the concept nicely and in detail.
 
 
subject: overriding and overloading