File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes override a static method / Cast Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "override a static method / Cast" Watch "override a static method / Cast" New topic
Author

override a static method / Cast

Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192
Hello,
There's the statement: Static methods cannot be overriden
This is not quite correct. You can override static methods but
they are hidden:
class Parent {
public static String getMessage()
{ return "I am the parent"; }
}
class Child extends Parent {
public static String getMessage()
{ return "I am the child"; }
}
public class Test2{
public static void main( String [] args ) {
Parent p = new Parent();
Child c = new Child();
System.out.println( p.getMessage() );
System.out.println( c.getMessage() );
Parent p2 = c; // upcast the child
System.out.println( p2.getMessage() );
// if overriding works, you should still
// see "I am the child."
}
}
if overriding works, you should still see "I am the child."
Result:
I am the parent
I am the child
I am the parent => No dynamic binding for static methods
This is because static methodes have compile time binding.
This means if they are changed during runtime by cast there
is no dynamic binding => Instead of "I am the child"
"I am the parent will be printed".
If there would be a dynamic binding as for non static/non final
methods => I am the child
Question:
An upcast of an object with a static method works. Why is there
a RUNTIME-Error if I do a downcast:
class Parent {
public static String getMessage()
{ return "I am the parent"; }
}
class Child extends Parent {
public static String getMessage()
{ return "I am the child"; }
}
public class Test2b{
public static void main( String [] args ) {
Parent p = new Parent();
Child c = new Child();
System.out.println( p.getMessage() );
System.out.println( c.getMessage() );
// Parent p2 = c; // upcast the child
Child c2 = (Child) p; // Now downcast the Parent
// System.out.println( p2.getMessage() );
System.out.println( c2.getMessage() );
}
}
------------------
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Originally posted by Thomas Markl:
Hello,
There's the statement: Static methods cannot be overriden
This is not quite correct. You can override static methods but
they are hidden:
This is because static methodes have compile time binding.
This means if they are changed during runtime by cast there
is no dynamic binding => Instead of "I am the child"
"I am the parent will be printed".
If there would be a dynamic binding as for non static/non final
methods => I am the child

Everything you said is true except that because they are hidden, static methods are not overridden they are shadowed. Much like variables.

Question:
An upcast of an object with a static method works. Why is there
a RUNTIME-Error if I do a downcast:
Parent p = new Parent();
Child c = new Child();
// Parent p2 = c; // upcast the child
Child c2 = (Child) p; // Now downcast the Parent

You can cast the subclass to the base class because the subclass 'is a' object of the same type as the base class. In your line Child c2 = (Child) p; the p variable references an object of Parent not Child, it 'is a' Parent it's not a child. If you change it to this then it do what your looking for:
Child c2 = (Child) p2; this works because p2 is actually referencing a variable of type Child.
hope that helps a little
Dave

Dave
Thomas Markl
Ranch Hand

Joined: Mar 08, 2001
Posts: 192

class Parent {
public static String getMessage()
{ return "I am the parent"; }
}
class Child extends Parent {
public static String getMessage()
{ return "I am the child"; }
}
public class Test2c{
public static void main( String [] args ) {
Parent p = new Parent();
Child c = new Child();
System.out.println( p.getMessage() );
System.out.println( c.getMessage() );
Parent p2 = c; // upcast the Child to Parent
Child c2 = (Child) p2; // Now downcast the Parent
// to Child
// System.out.println( p2.getMessage() );
System.out.println( c2.getMessage() );
// if overriding works, you should still
// see "I am the child."
}
}
Hello Dave,
thanks for your help. It was true.
If i upcast Child c to Parent p2 and then downcast
Parent p2 to a new Child object (c2) then the result
will be
I am the parent
I am the child
I am the child (Instead of "I am the parent)
Why does it now print "I am the child" e.g take subclass
static method?
Thomas

Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Originally posted by Thomas Markl:

Why does it now print "I am the child" e.g take subclass
static method?
Thomas

It prints the child method results because static methods are subject to early binding - they are called based on the type of the variable, not on the type of object referenced by the variable. So, in your code you're calling c2.getMessage(), c2 is of type Child so that's the method that gets called. If you uncomment the line before it
p2.getMessage() then the Parent method would get called because p2 is a Parent type variable, regardless of the type of object it references.
hope that helps
Dave

[This message has been edited by Dave Vick (edited June 25, 2001).]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: override a static method / Cast