aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes which method to call? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "which method to call?" Watch "which method to call?" New topic
Author

which method to call?

Kudret Serin
Ranch Hand

Joined: Aug 02, 2005
Posts: 167

Hi all,
I have a question from a mock exam.

The answer i choosed is B however the correct answer is A.
I thought, p is an instance of Director and the say() method of Director will be called at runtime (late binding). What is my mistake?
Thanks in advance for any explanation.
Ariel Ortiz
Ranch Hand

Joined: May 14, 2004
Posts: 121
The say method in class Director is not overriding the say method in class Person because it's private here (thus no late binging can take place).

...Ariel
Kudret Serin
Ranch Hand

Joined: Aug 02, 2005
Posts: 167

why does not the compiler complain that the method say() can not be overridden? (the signature of methods are identical, so this is an attempt to override, isn't it?)
Devender Thareja
Ranch Hand

Joined: Jul 14, 2005
Posts: 187
I tried it, the answer is correct.
The explanation that Person's say method is private, hence it's not overridden, is not correct. I changed it to public, still Person method is executing, not Director method.
It's not making any sense.


Devender Thareja
SCEA, SCBCD, SCJP
raghu babu
Ranch Hand

Joined: Jul 05, 2005
Posts: 60
I tried it, the answer is correct.

After changing the private to public in person's method, pl. make sure you compiled Director.java also (not just Person.java), as Director class is extending Person class and you made the change in a method of Person class and if Director class is not re-compiled, Director's byte code still got the Person class's old byte-code. Hope this make sense now
Devender Thareja
Ranch Hand

Joined: Jul 14, 2005
Posts: 187
Originally posted by vneeth babu:
I tried it, the answer is correct.

After changing the private to public in person's method, pl. make sure you compiled Director.java also (not just Person.java), as Director class is extending Person class and you made the change in a method of Person class and if Director class is not re-compiled, Director's byte code still got the Person class's old byte-code. Hope this make sense now


I am confused like hell.

I have tried 100 times. Deleted all *.class. Recompiled. I even modified the declaration of p to Director instead of Person. Still the output is "I'm saying: Hello".

My code
Director.java


Person.java
Alex Prawira
Greenhorn

Joined: Mar 05, 2005
Posts: 16
The point here is the reference variable.

You use the :
Person p = new Director();

See, the object is a Director. But the reference variable is typed for Person.
So the JVM will call the method say() from the Person Class.

try to alter the code to :
Director p = new Director();

Something different will happen.
Shivani Chandna
Ranch Hand

Joined: Sep 18, 2004
Posts: 380
Originally posted by Areca Thriller:
Hi all,
I have a question from a mock exam.
The answer i choosed is B however the correct answer is A.
I thought, p is an instance of Director and the say() method of Director will be called at runtime (late binding). What is my mistake?
Thanks in advance for any explanation.


I ran the code above and its working all fine - the answer is indeed A>
Wat gets printed is : I'm the director. Dont self doubt too much


/** Code speaks louder than words */
Devender Thareja
Ranch Hand

Joined: Jul 14, 2005
Posts: 187
Originally posted by Shivani Chandna:


I ran the code above and its working all fine - the answer is indeed A>
Wat gets printed is : I'm the director. Dont self doubt too much


The A is not "I'm the director", it's "I'm saying: hello".
For me it's indeed printing what A says. So the given answer is correct. Only it's hard to understand the reasoning. To me it seems that it should print "I'm the director".
I tried to use
Director p= new Director()
instead of Person p= new Director()
Still I am not getting the answer I expect "I'm the director".
The self doubt is increasing.
Bhaskar Singh
Greenhorn

Joined: Jul 23, 2005
Posts: 15
As Bert and Sierra yell in their SCJP book "which overridden method to call (in other words, from which class in the inheritance tree) is decided at runtime based on object type, but which overloaded version of the method to call is based on the reference type passed at compile time."
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8815
    
    5
did we really 'yell' that

I'm sorry, I hope we didn't hurt anyone's ears


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
satya mamillapalli
Ranch Hand

Joined: Jun 22, 2005
Posts: 63
say() method is neither overloaded nor overridden .. This should clear up the confusion.

Each class has their own say() method and at runtime method is form object type..

Satya
[ August 07, 2005: Message edited by: satya mamillapalli ]
Bhaskar Singh
Greenhorn

Joined: Jul 23, 2005
Posts: 15
the say method in Person is overridden by say method in the Director class. Since Director is a Person, we can override a method in Person with less restrictive access modifier such as public or protected or default( in this mock example, it is public). So my point is, access modifier is no problem here. The question wants to test your skill on decisions made by JVM on runtime and compile time based on object type and reference type when the methods are overridden or overloaded!
satya mamillapalli
Ranch Hand

Joined: Jun 22, 2005
Posts: 63
You can not inherit PRIVATE methods.How can we override the PRIVATE method? Please Advise..
Bhaskar Singh
Greenhorn

Joined: Jul 23, 2005
Posts: 15
Thanks satya for your insight....You're right, we can't override a method which can't be inherited. In this case both are separate methods!!
Bhaskar Singh
Greenhorn

Joined: Jul 23, 2005
Posts: 15
By the way Bert, I didn't mean that way!! You guys are like friends who're very brain friendly! I just didn't get the right word. I hope everybody knew the meaning of "yell" in that context. he he he...keep up the good works Author+s!!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: which method to call?