aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Overridding Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Overridding Doubt" Watch "Overridding Doubt" New topic
Author

Overridding Doubt

Lata Bagga
Ranch Hand

Joined: Jun 13, 2007
Posts: 35
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 ");
}
public static void main(String [] args) {
Animal [] a = {new Animal(), new Dog(), new Animal()};
for(int x = 0; x < a.length; x++)
a[x].doStuff(); // invoke the static method
}
}
Running this code produces the output:
a a a
---------------------------------------------------------------------------

In the above code it is said that static methods are redefined not overridden, and the output is also showing that the methods are not overriden but I am still confused that does redefining here means "Overloading".
Ian Edwards
Ranch Hand

Joined: Aug 14, 2006
Posts: 107
Static methods belong to the class rather than to the instance so they can't be overridden. Because your array is of type Animal it is invoking the doStuff() belonging to the class Animal.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Originally posted by Lata Bagga:

In the above code it is said that static methods are redefined not overridden, and the output is also showing that the methods are not overriden but I am still confused that does redefining here means "Overloading".


Because static methods don't behave polymorphically they cannot be overridden. Only redefined.
You may also overload them but this is something really different.

Bu.


all events occur in real time
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

"redefine" means defining it again. as simple as that. The method is actually not bothered or compared with any other similar versions of it in other classes in the inheritance hierarchy.

In your example, the static method in the subclass is treated as if its only in the subclass. Since the static method in the superclass belongs only to the superclass.

All it matters is the way you access. a[x].doStuff() and the compiler sees the variable with which you access (here it is "a") and its type (here its of type Animal).


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]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14420
    
  23

Note that the method is called doStuff with a capital S in class Animal, while you have a method dostuff with a lower-case s in class Dog. Is this a typo?


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

Thanks Jesper. You are correct.

But as per this question, seems to be of the standard SCJP question pattern. But still NOT to take it for granted!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Overridding Doubt