• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

override a static method / Cast

 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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() );
}
}
------------------
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Thomas Markl
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).]
 
And then the entire population worshiped me like unto a god. Well, me and this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic