• 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 methods invocation .....

 
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am very confused about the invocation of static methods, especially they are overridden in the sub class and accessing with super class reference.
Below i am giving the sample of question, please explain me the logic behind the output. My sincere request is, try to solve the programme without executing the programme and once after getting the solution then execute and compare your results. I am sure you will correctly solve the problem, unlike me ( Before executing the programme i will get one solution and after executing the programme i will get another solution, then i will give some bluff reason for the solution and soon will forget the logic, i am sure you are not like that).
Here is the programme.
--------
class P {
static void printS1(){System.out.println("P.printS1 ");}
void printS1S2(P w){printS1();}
}
public class Q extends P {

static void printS1(){System.out.println("Q.printS1 ");}
void printS1S2(P w){
w.printS1(); // (4)
printS1();}// (5)
public static void main(String[] args) {
new Q().printS1();//(1)
P p = new Q();
p.printS1();//(2)
p.printS1S2(p);// (3)
}}
----
Please tell me if some more complications are existing with static methods.
Thanks in Advance,
Narasimha.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I usually look at these programs starting from the beginning, so let's look at main():

Line (1) calls Q.printS1() which displays "Q.printS1".
Line (2) calls P.printS1() since p is an instance of the P class. Even though P extends Q, static methods are NOT inherited and don't take part in the polymorphism mechanism. This means the output will be "P.printS1".
Line (3) calls Q.printS1S2() because this method isn't static. Since p really references an object of type Q, polymorphism takes effect.

Line (4) calls P.printS1() since w is a reference to a P object (at least as far as the compiler knows). This results in "P.printS1" being displayed.
Line (5) on the otherhand calls Q.printS1 since the current object is of type Q. This results in "Q.printS1".
Putting this altogether, the output should be
Q.printS1
P.printS1
P.printS1
Q.printS1
I haven't ran the program yet, and since you didn't post the results, I'm not quite 100% sure this is correct. If not, I sure hope someone will correct any errors in my logic.
Layne
[ February 18, 2004: Message edited by: Layne Lund ]
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Narasimha,
Layne is absolutely right. I ran the program and checked the output.
While preparing for SCJP, I made one simple table to remember the concept of static method overriding.
-----------------------------------------------------------------------
Superclass......Subclass........Compliles?......Polymorphism applies?
-----------------------------------------------------------------------
Non-static......Non-static......Yes.............Yes
Non-static......Static............No................-
Static............Non-static......No................-
Static............Static............Yes..............NO
-----------------------------------------------------------------------
Hope the above table helps.
Have a nice day. Bye.
[ February 18, 2004: Message edited by: Sandeep Achar ]
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can somebody explain the output of this and why - in light of above discussion?

Thanks
Bijesh
 
Narasimha Rao B.
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bijesh,
Answer will be, Parent -- aMethod, Child -- anotherMethod followed by Child -- aMethod. Below is the explanation,
1. When you say, p1.aMethod() - aMethod() is static, hence it will be invoked from the p1 reference variable's class, which is nothing but Parent class, hence Parent's aMethod() will be invoked and the output is - Parent -- aMethod
2. When you say, p1.anotherMethod() - anotherMethod() is non-static, hence it will be invoked from the run time type of the object, which is nothing but new Child(), hence anotherMethod() from the Child class will be invoked and the output is Child -- anotherMethod.
When you call aMethod() from anotherMethod() from Child class, actually it will be resolved to - this.aMethod(), and this will refer the object on which anotherMethod() is called which is nothing but new Child(), hence aMethod() of Child class will be invoked, hence the output, Child -- aMethod.

I am not sure whether you understood or not( Because of my explanation). You can come up with new set of questions, most welcome.
 
Bijesh Krishnadas
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by narasimha rao bandlamudi:

2. When you say, p1.anotherMethod() - anotherMethod() is non-static, hence it will be invoked from the run time type of the object, which is nothing but new Child(), hence anotherMethod() from the Child class will be invoked and the output is Child -- anotherMethod.
When you call aMethod() from anotherMethod() from Child class, actually it will be resolved to - this.aMethod(), and this will refer the object on which anotherMethod() is called which is nothing but new Child(), hence aMethod() of Child class will be invoked, hence the output, Child -- aMethod.


In other words within a non-static method (here anotherMethod), the current object(this) will not be considered as the Parent object holding the Child reference, but as a genuine Child object...
Get what I mean??? And if u do, am I right?
Bijesh
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bijesh, Haven't you read the explanation above my code post here !!! If so what is the part you don't get?
 
Narasimha Rao B.
Ranch Hand
Posts: 205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Bijesh Krishnadas:

the current object(this) will not be considered as the Parent object holding the Child reference, but as a genuine Child object...

Bijesh



Hi Bijesh,

I couldn't get, what exactly you mean in the above sentence?
[ February 19, 2004: Message edited by: narasimha rao bandlamudi ]
reply
    Bookmark Topic Watch Topic
  • New Topic