• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

how is this?help please

 
venka sai
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


output: DCBA




OUTPUT : DDDD

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
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
methods are dynamically bound and variables are bound at compile time
 
bhavesh bhanushali
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ,
bhavesh
 
Marcelo Ortega
Ranch Hand
Posts: 528
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Georgy Bolyuba
Ranch Hand
Posts: 162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator






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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic