Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

which method to call?

 
Kudret Serin
Ranch Hand
Posts: 167
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 121
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 167
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 187
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
raghu babu
Ranch Hand
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 187
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 380
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Devender Thareja
Ranch Hand
Posts: 187
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8898
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
did we really 'yell' that

I'm sorry, I hope we didn't hurt anyone's ears
 
satya mamillapalli
Ranch Hand
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 63
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can not inherit PRIVATE methods.How can we override the PRIVATE method? Please Advise..
 
Bhaskar Singh
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic