This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin for Android App Development and have Peter Sommerhoff on-line!
See this thread for details.
Win a copy of Kotlin for Android App Development this week in the Kotlin forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Overridding Doubt  RSS feed

 
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".
 
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.
 
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.
 
Ranch Hand
Posts: 3389
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).
 
Java Cowboy
Posts: 16084
88
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: 3389
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!
 
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database
https://coderanch.com/t/704633/RavenDB-Open-Source-NoSQL-Database
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!