• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Overridding Doubt

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic