• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

static vs non static overriding

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can somebody please explain the difference in overriding with regard to static and not static methods ?
Thanks :
 
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In non-static overriding, you are overriding the method of the super class. That means that you are going to declare the same method name with same argument types and return types. And, at runtime, according the instance that calls it, you select the method of either super or subclass.
In static overriding, its pseudo overriding or rather its not overriding at all. Even if you have two similar methods in super and sub classes, the super classes method and the subclasses method are two completely different methods which happen to have everything else as same coincidentally.
Hope this helps
Sri
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yup, basically, you can't override a static method. It belongs to the class, not the instance. So when you extend a class and make a subclass -- you never inherit the static method -- so you can't override it.
And becareful -- this is one of the favorite questions on the SCPJ exam (according to Kathy and Bert's Certification book).
So, if you see something like this:

And the question is...
What is the result of attempting to compile and run this program?
a - compile time error
b - run time error
c - prints Car.start()
what would you answer??
 
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's a good one Jessica! I'll add a less interesting variation just to round out the topic : )

What is the result? (choose one)
A. Runtime error
B. Compiler error
C. Jeep.start()
Car.start()
D. Jeep.start()
Car.start()
: )
 
Sridhar Srikanthan
Ranch Hand
Posts: 366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kathy,
Thats a great example
Thanks
Sri
 
bobby chaurasia
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ans to part 1: compile error
part 2: Jeep.start()
Car.start()
To be honest my ans for first was not right.
I overlooked the fact that super and this can not be used for static methods.
So when we say a static method can be overridden by another static method, its not actually overriding so why not just say a static method can not be overridden ?
Thanks a lot...
[ January 12, 2003: Message edited by: bobby chaurasia ]
 
Kathy Sierra
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Howdy -- I agree that we should NOT call it overriding. Overriding implies polymorphism, for one thing (and virtual method invocation), which we obviously do NOT get with static methods.
But some people *do* use the term overriding more loosely, to mean "redefining a method in a subclass that matches the method in the superclass..." and with that (incorrect, in my opinion definition, then it does *appear* that you can override a static method.
I agree, again, that we should NOT call it overriding, but rather "redefining in a subclass", kind of like the way that you can "redefine a variable in a subclass" but that does not mean you are "overriding an instance variable."
Cheers,
-Kathy
"If you like it, don't tell me, tell Amazon"
- a starving writer,
(referring to reviews on Amazon)
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or, heck, we could even call it "hiding". Though I admit that's less descriptive than "redefining in a subclass". But it has the advantages of being (a) shorter and (b) official.
 
Greenhorn
Posts: 28
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jessica,
You said:
So when you extend a class and make a subclass -- you never inherit the static method -- so you can't override it.
My question is if static method can't be inherited why I can compile the following code and run?
class Car {
static void start() {
System.out.println("Car.start()");
}
static void carStart(){
System.out.println("CarStart");
}
}
class Jeep extends Car {
static void start() {
// super.start();
System.out.println("Jeep.start()");
}
void start1(){
super.start();////1)I can use super here.
}
public static void main (String[] args) {
Jeep jeep = new Jeep();
jeep.start();
jeep.start1();
jeep.carStart(); ////2)I can call Car's static method carStart() here.

}
}
Sam
 
bacon. tiny ad:
Clean our rivers and oceans from home
https://www.kickstarter.com/projects/paulwheaton/willow-feeders
reply
    Bookmark Topic Watch Topic
  • New Topic