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

static methods/casting question

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Given the code below:
class Super {
static String greeting() { return "Goodnight"; }
String greetingx() { return "Goodnight"; }
String name() { return "Richard"; }
}
class Sub extends Super {
static String greeting() { return "Hello"; }
String greetingx() { return "Hello"; }
String name() { return "Dick"; }
}
class TestStat {
public static void main(String[] args) {
Super s = new Sub();
System.out.println(s.greeting() + ", " + s.name());//Goodnight, Dick
System.out.println(((Sub)s).greeting() + "," +s.name());//Hello,Dick (1)
System.out.println(((Super)s).greeting() + ", " + s.name());//Goodnight,Dick (2)

System.out.println(((Sub)s).greetingx() + ", " + s.name());//Hello, Dick
System.out.println(((Super)s).greetingx() + ", " + s.name());//Hello, Dick

Super s1 = new Super();
//System.out.println(((Sub)s1).greeting() + ", " + s1.name());//ClassCasTException
}
}
in comments in front of the SOP's I showed the output.
I know that static methods calls are based on the reference type, and that instance methods are called based on the type of the object pointed to by the refernce type.
I also know that a cast does not change the class of an object; it only checks that the class is compatible with the specified type.
Can someone please explain me in detail how the output for the static method calls results in (1) and (2)
I thought we should get for both Goodnight Dick
Does the cast operator have an influence if the methods are static instead of instance ???
Thanx
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
JLS Spec 8.6.4.2:

If a class declares a static method, then the declaration of that method is said to hide any and all methods with the same signature in the superclasses and superinterfaces of the class that would otherwise be accessible to code in the class. A compile-time error occurs if a static method hides an instance method. In this respect, hiding of methods differs from hiding of fields (�8.3), for it is permissible for a static variable to hide an instance variable.
A hidden method can be accessed by using a qualified name or by using a method invocation expression (�15.11) that contains the keyword super or a cast to a superclass type. In this respect, hiding of methods is similar to hiding of fields.

By casting s back to a Sub you access the method which hides Super.greeting(). If you continued subclassing and hiding the greeting() method, you would always access the method defined in the type of the reference.
[ April 04, 2004: Message edited by: Michael Morris ]
 
Dan Andrei
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanx Michael,
even if I went through the JLS twice the final part of the 2nd paraghraph you quoted never stuck in my mind:
"... or a cast to a superclass type. " which is crucial for my example
 
Whatever. Here's a tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic