• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Overridding Doubt

 
Lata Bagga
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1274
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"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).
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15495
43
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic