• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

this #2

 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I don�t have any questions. I am just trying to confuse someone.
[ April 11, 2003: Message edited by: Marlene Miller ]
 
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Marlene
I were confused
The answer:
B
2
1
1 ?
1 ?
Please let me know if I answer correct or wrong
thanks
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bear in mind that fields are statically bound, ie resolved at compile time. So, when t(this)).x is cast to B, the compiler grabs the value of 2. Similarly, the cast to C resolves to 3. Hence, the last three printouts are:
1
2
3
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Roger is right.
Let's see the compile type of the expressions on which the field access axpressions occur. Because we are accessing fields those type will determine at compile time the variable the expression accesses:
this.x
The compile type of this is B. Thus B.x yields 2.
t(this).x
The compile type of t(this) is the one of its return type, that is A. Thus A.x yields 1
(B)t(this).x
The compile type is B, yielding 2
(C)t(this).x
The compile type is C. Thus it yields 3
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Jose. You always have such a nice and clear way of explaining. And always JLS-accurate.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Revised...
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oo.... let me try again
B
2
1
2
3
10
30
oo.... Is it right?
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, this.y in B inherits y from A, so answer is 10.
 
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
The answer should be:
C // not B
2
1
2
3
10
30
but could anyone tell me how to distinguish compile time and runtime when yeild a field value ?
Thanks!
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You always get runtime binding for overridden methods. What can be overridden? Methods which are not, either explicitly or implicitly:
- static
- final
- private (implicitly final)
Note that a method of a class which is declared private or final cannot be inherited, let alone overridden.
For anything else, it's compile-time binding.
 
Jack Lau
Ranch Hand
Posts: 168
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much !
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi everybody
The answer should be:
B
Am I right?
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It�s C. Would you like to try again?

Here are the clues (in cryptic JLS jargon):
1. When used as a primary expression, the keyword this denotes a value, that is a reference to the object for which the instance method was invoked, or to the object being constructed.
2. The type of "this" is the class X within which the keyword this occurs.
3. At run time, the class of the actual object referred to may be the class X or any subclass of X.
[ April 13, 2003: Message edited by: Marlene Miller ]
 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!
The answer should be:
C
C
B
B
Please correct me if i am wrong.
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Marlene
thanks for your time to answer
yes this time the answer should be
C
C
----
B
B
thanks for attention
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And the winners are...Anushka and Siu. CCBB
I am almost done. Only a few more. This next one is tricky.
Quiz #4

The hint, as always, is JLS 15.8.3. That is what I am trying to understand.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Quiz #5. This one is straight-forward. No tricks.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Quiz #6. Another tricky one.
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is what I think is happening for quiz 6.
The first three printouts will be the runtime type of class C, ie, C, C, C.
But it will fail at runtime on C c = (C)this; when new B().m(); is invoked because you cannot downcast B to C.
Nice quiz.
[ April 14, 2003: Message edited by: Roger Chung-Wee ]
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For #5, the first two print statements involve methods, so they will both use the runtime type and print out:
C
C
The real issue is this.x in the third print statement. Since in the class B this is of type B, the hiding of x by C is irrelevant and the printout will be:
1
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by siu chung man:
hi Marlene
I were confused
The answer:
B
2
1
1 ?
1 ?
Please let me know if I answer correct or wrong
thanks


B is not correct. The correct answer is C
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Marlene
thanks for your time to give more quiz
Quiz #4
Answer:
B
B

Quiz #5
Answer:
Test
C
3

Quiz #6
Answer:
C
B
C
B
B
C

Am I 100% right
Once again
thanks for your time to shire this quiz to us
 
Anushkha Rana
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All!
This I think should be the answers:
Quiz #4
-------
compiler will complain at line B b = this; because base class reference cannot be assigned to derived class reference without explicit casting. At compile time, keyword this within method m() denotes object of class A.
if its changed to B b = (B)this; then output should be:
B
B
Quiz #5
-------
output should be:
C
C
1
Quiz #6
-------
output should be:
C
C
C
and ClassCastException at C c = (C)this; because base class object cannot be denoted by derived class reference.
Right, Marlene??
[ April 15, 2003: Message edited by: Anushkha Rana ]
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Quiz 4:
: incompatible types
found : A
required: B
B b = this;
^
1 error
Quiz 5:
C
C
1
Quiz 6:
class C
class C
class C
java.lang.ClassCastException
at A.m(Test13.java:5)
at Test.main(Test13.java:18)
Exception in thread "main"
Thank you Siu, Roger, Jose, Jack, Anushkha, Bryan and Badrinath for sharing in my quest to understand �this�. It was fun.
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is what I was trying to understand and feel comfortable with:
�this� has a type known at compile time. The type of �this� is the class within which the keyword �this� occurs. The type of �this� might be different from the type of the object �this� refers to.
Quiz 4: The type of �this� is A. �this� refers to an object of type B.
Quiz 5: The type of �this� is B. �this� refers to an object of type C.
Quiz 6: The type of �this� is A. �this� refers to an object of type C, then an object of type B.
this.getClass() represents the type of the object that �this� refers to.
Thanks again for your interest.
 
Francis Siu
Ranch Hand
Posts: 867
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Marlene
I am grateful if you have time to give more quiz,
because I want to know more about "this".
thanks
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Siu, I would be glad to think up some more questions. I like helping people to learn. I'll try to figure out where you get confused. I'll start fresh with a new thread. Marlene
reply
    Bookmark Topic Watch Topic
  • New Topic