It's not a secret anymore!
The moose likes Java in General and the fly likes Overloading & Overriding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Overloading & Overriding" Watch "Overloading & Overriding" New topic

Overloading & Overriding

Fred Barnes
Ranch Hand

Joined: Jun 11, 2002
Posts: 62
I have a question that concerns overriding. When you override a method it can not
be more restrictive or throw any checked exceptions of classes that are not possible
for the original method. Correct me if I am wrong, but you can override a non-static method
to be static?
1. Am I correct?
2. My question is if you can override a static method to be non-static?
3. How does this affect overloading in terms of static and non-static methods?
Kind Regards
Juanjo Bazan
Ranch Hand

Joined: Feb 04, 2002
Posts: 231
The overriding method can have a different throws clause as long as it doesn't declare any types not declared by the throws clause in the overridden method. Also, the access specifier for the overriding method can allow more access than the overridden method, but not less.
About your question 2: a subclass cannot override methods that are declared static in the superclass. In other words, a subclass cannot override a class method. A subclass can 'hide' a static method in the superclass by declaring a static method in the subclass with the same signature as the static method in the superclass.

khella smith
Ranch Hand

Joined: May 06, 2002
Posts: 39
u can't override a static method to be non-static neither can u override a non-static to be static
u can only redeclare a static method to be the same as the one in the superclass just like bazan sometimes look like u are overriding a static method with a static method but u are only redeclaring. in overloading it is a different ballgame u can read more in\tutorial
or java sun's tutorials
[ June 18, 2002: Message edited by: khella smith ]
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
From the Java Language Specification

A compile-time error occurs if an instance method overrides a static method.

If a class declares a static method, then the declaration of that method is said to hide any and all methods with the same signature in the superclasses and superinterfaces of the class that would otherwise be accessible to code in the class. A compile-time error occurs if a static method hides an instance method.

"JavaRanch, where the deer and the Certified play" - David O'Meara
I agree. Here's the link:
subject: Overloading & Overriding
It's not a secret anymore!