wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static methods cannot be overridden ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static methods cannot be overridden ?" Watch "Static methods cannot be overridden ?" New topic
Author

Static methods cannot be overridden ?

Sizhnor Rhena
Greenhorn

Joined: Nov 17, 2003
Posts: 4
I am getting confused with overridden Vs. redeclared/ redefined ?
K& B book Say:
"Static methods cannot be overridden, although they can be redeclared/ redefined by a subclass. So although static methods can sometimes appear to be overridden, polymorphism will not apply "
so in this code below //2 is overridden or redeclared/ redefined ?
class Car {
public static void drive(){ //1
System.out.println("I drive Car");
}
}
class Hammer extends Car {

//The variables in java can have the same
//name as method or class
static String Hammer = "Arnold";
public static void drive(){ // 2
System.out.println("I drive Hammer!");
System.out.println(Hammer);
}
static void main(String[] args){
Hammer H2 = new Hammer();
//call drive method from parent class Car
Car.drive();//upcasting
//call drive method from child class Hammer
H2.drive();
}
}
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
Welcome to JavaRanch, Sizhnor!
Static methods are class bound rather than runtime object type bound. That means they cannot be overridden since polymorphism doesn't hold. Which static method is called is defined at compile time based on the class name or declared reference type. You can not redefine a static method to be non-static in a subclass, and the opposite is true for non-static methods in a superclass - it cannot become static in derived classes.
Sizhnor Rhena
Greenhorn

Joined: Nov 17, 2003
Posts: 4
In the code below drive()method in parent and child both are static so what I undersatan from previous posting is static method can't be overridden , redeclared or redefined . So what is drive() method in child class in the code below ?
class Car {
public static void drive(){ //1
System.out.println("I drive Car in parent class");
}
}
class Hammer extends Car {

public static void drive(){ // 2
System.out.println("I drive Hammer in child class!");
}
static void main(String[] args){
Hammer H2 = new Hammer();
//call drive method from parent class Car
Car.drive();//upcasting
//call drive method from child class Hammer
H2.drive();
}
}
[ November 17, 2003: Message edited by: Sizhnor Rhena ]
[ November 17, 2003: Message edited by: Sizhnor Rhena ]
Vad Fogel
Ranch Hand

Joined: Aug 25, 2003
Posts: 504
Static methods can be redefined (redeclared), but they cannot be overridden.
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
Howdy -- let me see if I can add a little more... (especially since I'm the one that probably made it confusing in the book )
Normally, when we say "override" we mean the following;
* Redefine a non-static method in a subclass
(which of course means we have certain rules to follow like we can't change the args or return type, and we can't have more restrictive access or throw new or broader checked exceptions)
And when we do that, it means that the version of the method that runs (parent's version vs. child version) depends on the actual object type, not the reference type.
Normal overridden method;
public class Foo {
void go() { }
}
public class Bar extends Foo [
void go() { } // overrides the parent class method
}

// somewhere else...
Foo f = new Bar();
f.go(); // the Bar version runs, because Bar is the *object* type, even though Foo is the *reference* type - that's polymorphism in action.
But with a static method, the polymorphism does not apply.
public class Foo {
static void go() { }
}
public class Bar extends Foo [
static void go() { } // redeclares it, but this is not considered an override (even though it looks like it)
}
Foo f = new Bar();
f.go(); // this time the Foo version runs, even though Bar is the object type. Static methods are bound at compile time based on the *reference* type, which is Foo. So this tells us that the static go() method was not *really* overridden.
When you think "override", think "polymorphism", think "object type, not reference type"
cheers,
Kathy
Sizhnor Rhena
Greenhorn

Joined: Nov 17, 2003
Posts: 4
Thanks Vad & Kathy !
Now It is clear to me.
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
One thing I like about C# is that in C# you must use a Class name to run static methods. Allowing you to use an object name to run a class method creates way too much confusion for people taking the certification exam!


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Cathy Song
Ranch Hand

Joined: Aug 24, 2003
Posts: 270
The following article really helped me..

How my Dog learned Polymorphism

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static methods cannot be overridden ?