• 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:

static hidden methods, and overriden instance methods

 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the question from the mock exam:

What happens?
The answer is Prints: P.printS1 Q.printS2
Ok I can accept this answer at face value...but what is the rule. This seems like a totally arbitrary rule...but can somebody give me an overriding rule that makes sense, in terms of rationally why this should be. I don't believe the study guide I've been using has anything close to as obscure a question as this.
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is essential to realise that the compiler will sort out whatever it can at compile-time, like anything static or overloaded methods. But overridden methods are sorted out at runtime.
Here we have this:
void printS1S2(){printS1();printS2();}
printS1() of class P is statically bound as it is not overridden. This gives you "P.printS1 ".
On the other hand, printS2() is overridden and is invoked depending on the type of object (Q). This gives you "Q.printS2 ".
[ April 24, 2003: Message edited by: Roger Chung-Wee ]
 
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Method printS1S2() is inherited by subclass Q.
The compiler sees that printS1() is static and looks no further => method invocation resolved at compile time.
Method printS2() is an instance method => compiler creates a virtual method invocation that is resolved at runtime based on the actual object.
These programs ca be made easier to understand by taking 1 rule at a time.
below are 2 examples.
Program 1: static methods only.

Program 2: combines override and static.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
void printS1S2(){printS1();printS2();}
is the same as
void printS1S2() { this.printS1(); this.printS2() );
I guess the confussion steems from not knowing wich class to look for printS1, either P , or Q?.
Always begin the search of the method to invoke by the class that is the compile type of of the expression on which the method invocation occurrs.
this is the expression on which the method invocation occurs. The compile type of this is the class declaring the method where this happens to be. That is P. Search P for printS1 declaration, there is one. Stop there, coz printS1 is static you do not have to look at subclasses.
Now, when printS2 is located in P, trying to resolve which printS2 method is called, we note that is not static, so we look at the runtime type of this. In this.printS2(), this is a reference to an instance of Q. Which one? the one created by new Q() in main. In the class Q there is a method overriding P.printS2, that method is called on the instance hold by this.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic