This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
Please explain Various outputs in following program? class Testing extends Object{ public static void main(String args[]){ Shape p = new Shape(); Shape q= new SubShape(); System.out.println("p.y:"+p.y); //line#1 output is : 7 System.out.println("q.y:"+q.y); //line#2 output is : 7, why not 5? System.out.println("p.gety:"+p.gety()); //line#3 output is : 7 System.out.println("q.gety:"+q.gety()); //line#4 output is : 5, why so ?
Shape r = q; System.out.println("r.y:"+r.y); //line#5 output is : 7 SubShape t = p; //line#6 compile time error SubShape t1 = (SubShape)p; //line#7 run time error ClassCastException System.out.println("t1.y:"+t1.y); //line#8 SubShape h = (SubShape)r; //line#9 why no runtime error here as on line#7? System.out.println("h.y:"+h.y); //line#10 Output is:5
Shape r1 = p; SubShape h1 = (SubShape)r1; //line#11 runtime error here but not in line#9 ? System.out.println("h1.y:"+h1.y); //line#12 }//end of main } //end of class Testing class Shape { int y =7; int gety(){ return y; } }
class SubShape extends Shape { int y =5; int gety(){ return y; } } Thanx Richa
Valentin Crettaz
Gold Digger
Sheriff
Joined: Aug 26, 2001
Posts: 7610
posted
0
when accessing member variable it is always the DECLARED type that counts and not the runtime type... when you declare Shape p = new Shape(); Shape q = new SubShape(); p.y and q.y will refer to the same values since their declared type is Shape. When accessing member method, it is always the RUNTIME type that counts and not the declared type, thus p.gety() will invoke gety() of Shape q.gety() will invoke gety() of SubShape Is it clearer now ?? HIH ------------------ Valentin Crettaz Sun Certified Programmer for Java 2 Platform
thanx val I am still not clear about line#7 and line#9
Valentin Crettaz
Gold Digger
Sheriff
Joined: Aug 26, 2001
Posts: 7610
posted
0
sorry... line 6: p is of type Shape thus a cast is necessary to compile since you can't assign an object of type superclass to a reference of type subclass line 7: to go around the compile error on line 6 you provide the cast, but since p is of type Shape you cannot cast it to type SubShape thus yielding a ClassCastException. If you cast something you tell the compiler that you are sure that the object you are casting will be (at runtime) of the type to be cast to. This is not the case here since again p is of type Shape and not SubShape. line 9: r is of declared type Shape but of runtime type SubShape, because q is of runtime type SubShape and gets assigned to r (just before line 5) line 11: r1 is of runtime type Shape and thus same explanation than for line 7. HIH ------------------ Valentin Crettaz Sun Certified Programmer for Java 2 Platform [This message has been edited by Valentin Crettaz (edited October 03, 2001).]
Richa Jeetah
Greenhorn
Joined: Sep 27, 2001
Posts: 29
posted
0
Thanx a lot Val You are a life saver........I had serious confusion ..not anymore
Valentin Crettaz
Gold Digger
Sheriff
Joined: Aug 26, 2001
Posts: 7610
posted
0
You are a life saver
Oh, I wouldn't say that, but thanks ------------------ Valentin Crettaz Sun Certified Programmer for Java 2 Platform
Ajit B
Greenhorn
Joined: Jul 24, 2001
Posts: 28
posted
0
when accessing member variable it is always the DECLARED type that counts and not the runtime type... When accessing member method, it is always the RUNTIME type that counts and not the declared type,
hi Valentin........ just small question. i dint get the reason y java has been implemeted this way. isn't it mean , when v upcast the object v can call methods of subclass and can call attributes of superclass? isn't it mean by this way v r losing access to some of public attributes in subclass? thx in advance .........Ajit
Ajit B, Please read the JavaRanch Name Policy and re-register using a name that complies with the rules. Thanks for your cooperation. ------------------ Jane Griscti Sun Certified Programmer for the Java� 2 Platform