• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Question on inheritance from Dan's exam

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class P {
static void printS1(){System.out.print("P.printS1 ");}
void printS2() {System.out.print("P.printS2 ");}
void printS1S2(){printS1();printS2();}
}
class Q extends P {
static void printS1(){System.out.print("Q.printS1 ");}
void printS2(){System.out.print("Q.printS2 ");}
public static void main(String[] args) {
new Q().printS1S2();
}}
For the code above, the answer is
Prints: P.printS1 Q.printS2
The explaination says that If the method called is a static member of the class where the invocation expression occurs, then that is the implementation of the method that is invoked at run-time regardless of the run-time type of the object but the method printS1S2()is also a part of the class Q which inherits it from class P so shouldn't the method printS1() of class Q be called and the answer should be Q.printS1 Q.printS2?
Pls help me understand.
Thanks.
 
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Geeta,
Thank you for using my exam.
Your question come up frequently here at the Ranch. If you search on printS1S2 you will find lots of threads. (There's a search option near the top of the page.) Here's the most recent thread.
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
void printS1S2(){printS1();printS2();}
I sometimes wonder whether this is a valid way to explain why the rule is what it is:
In Java, class methods are bound at compile-time (static binding) and instance methods are bound at run-time (dynamic binding).
At compile-time, the method invocation printS1 is bound to the implementation printS1 in class P.
At run-time, the method invocation printS2 is chosen by the dynamic lookup process and bound to the implementation printS2 in class Q of the object.
 
geeta rai
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Dan and Marlene Miller for your help.
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If u have a superclass and a subclass both having the same method and both marked static, then Polymorphism will not apply as in ur example because the static methods are not overriden but if the two methods r not static then the subclass method is overriding the superclass method and polymorphism will apply
reply
    Bookmark Topic Watch Topic
  • New Topic