Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes what is the difference between redefining and overriding a method in a subclass Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "what is the difference between redefining and overriding a method in a subclass" Watch "what is the difference between redefining and overriding a method in a subclass" New topic
Author

what is the difference between redefining and overriding a method in a subclass

rakhee gupta
Ranch Hand

Joined: May 01, 2008
Posts: 43
What is the difference between overriding and redefining.
Following example says:

class Animal {
static void doStuff() {
System.out.print("a ");
}
}
class Dog extends Animal {
static void dostuff() { // it's a redefinition,not an override
System.out.print("d ");
}

But dostuff method according to me is overriden here.
Please clarify.
Marc Torpe
Greenhorn

Joined: Jul 11, 2008
Posts: 5
http://faq.javaranch.com/view?OverridingVsHiding
rakhee gupta
Ranch Hand

Joined: May 01, 2008
Posts: 43
Thank you.Its clear now.
Rustam Boltaev
Greenhorn

Joined: Apr 12, 2009
Posts: 3


Thanks for the link, that helped me too. But now I have another question: What's the point of hiding(redefining?) a static method if there is no polymorphism involved, and if one can always access the original static method through a class, its instance, or an instance of its subclass?
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

"Rustam BT " welcome to Javaranch
please check your private messages for an important administrative matter. You can check them by clicking the My Private Messages link above.

The same FAQ wrote:With classMethod() though. since (ahem) it's a class method, the compiler and JVM don't expect to need an actual instance to invoke the method. And even if you provide one (which we did: the instance referred to by f) the JVM will never look at it. The compiler will only look at the declared type of the reference, and use that declared type to determine, at compile time, which method to call. Since f is declared as type Foo, the compiler looks at f.classMethod() and decides it means Foo.classMethod. It doesn't matter that the instance reffered to by f is actually a Bar - for static methods, the compiler only uses the declared type of the reference. That's what we mean when we say a static method does not have run-time polymorphism.


That is the reason why "overriding" a static method actually "hides" it.


SCJP, SCWCD.
|Asking Good Questions|
Rustam Boltaev
Greenhorn

Joined: Apr 12, 2009
Posts: 3
Thanks for the warm welcome , it's good to be here.

I think now I understand how and why an "overridden" class method actually becomes "hidden", thanks to you and the study guide. My only problem is that I can't think of any single case where one would want to "hide" a static method (for a good design, effectiveness, reuse or any other reason). It actually seems redundant to me.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: what is the difference between redefining and overriding a method in a subclass
 
Similar Threads
Static methods : Difference between redefining and overriding ?
K&B book doubt on page 147
Override Vs Redefinition
Difference between redefinition and overridding
override and redifinition?