• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static methods cannot be overridden ?

 
Sizhnor Rhena
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 504
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Static methods can be redefined (redeclared), but they cannot be overridden.
 
Kathy Sierra
Cowgirl and Author
Rancher
Posts: 1589
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Vad & Kathy !
Now It is clear to me.
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Cathy Song
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following article really helped me..

How my Dog learned Polymorphism

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic