wood burning stoves
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes how is this?help please 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 "how is this?help please" Watch "how is this?help please" New topic

how is this?help please

venka sai

Joined: Jul 17, 2005
Posts: 2

output: DCBA


How it is the first one can access the value depends on the runtime casting type.but we are doing the same in the second example.but we are getting different output.here also it should consider the runtime casting type right?please explain me.

[ July 17, 2005: Message edited by: venka sai ]
[ July 17, 2005: Message edited by: Barry Gaunt ]
Girish Bal
Ranch Hand

Joined: Jun 23, 2005
Posts: 77
methods are dynamically bound and variables are bound at compile time

Girish B
SCJA 1.0 (86%)
SCJP 1.4 (91%)
bhavesh bhanushali
Ranch Hand

Joined: Jun 13, 2005
Posts: 55
hey i think the reason is that you can access hidden variables of super classes which are more then one level high , but you cannot acces overridden methods of superclasses which are more than one level from the subclass

by one level i mean the immeadiate super class

regards ,
Marcelo Ortega
Ranch Hand

Joined: May 31, 2005
Posts: 528

Can somebody please explain the above code pieces in detal, coz i thought that the method is chosen dynamically at runtime depending on the type of the object, but in the second code snippet, the objects are being cast to higher up the inheritance heirarchy but yet dynamic binding doesn't seem to be in play here.

As for the first code snippet, i thought the answer would of been DDDD because the TYPE of the variable isn�t specified so i assumed it would of been of type D, and when we cast it, your only changing the object, and not the TYPE.

Please clear this up, or at least the second code snippet.

Thanx in advanced, Marzo.


Live life to an interface, not an implementation!
Georgy Bolyuba
Ranch Hand

Joined: Feb 18, 2005
Posts: 162

First, let's look at first code. Expression "this.s1" is clear for everybody I guess. Then we cast "this" like this "((C)this)". Now what we have here? Reference to object of type "C". We could do it like this:"C c = this;". This is legal beacuse "this" is an object of type D and D is a subclass of C. So far so good.

Now we trying to access s1 variable."((C)this).s1" is equal to "C c = this; c.s1". But we have more then one s1 (one in D class and enother in C class). Which one will be accessed?

Compiler sees the type of referens variable (which is type C) and bound s1 with variable which is declared in C class. And, as a result, you get value of s1 from C class (which is "C").

Let's take a look on the second piece of code.

This story is complete different. Then you call "((C)this).m1();" it is equal to "C c = this; c.m1();". But for now which method will be invoked will be solved at runtime, by JVM not by a compiler. So, JVM looks at you object and founds that the type of it is D!!! And JVM calls method m1() from D class. So, you get your "DDDD" output.

(Now I'm gonna kill myself for explanation of that kind )

P.S. Answers are correct.
[ July 19, 2005: Message edited by: George Bolyuba ]

SCJP 1.4 (100%) Done.<br />SCJD (URLyBird 1.2.3 Started)
I agree. Here's the link: http://aspose.com/file-tools
subject: how is this?help please
It's not a secret anymore!