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.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Sub classes And casting Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "Sub classes And casting" Watch "Sub classes And casting" New topic
Author

Sub classes And casting

Richa Jeetah
Greenhorn

Joined: Sep 27, 2001
Posts: 29
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
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


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Richa Jeetah
Greenhorn

Joined: Sep 27, 2001
Posts: 29
thanx val
I am still not clear about line#7 and line#9
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
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
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

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


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
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
The reason is called polymorphism:
http://codeguru.earthweb.com/java/tij/tij0077.shtml


SCJP2. Please Indent your code using UBB Code
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
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


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Sub classes And casting
 
Similar Threads
IBM Mock test
Object behavior when reassign !
Overridding
Dynamic Binding????????
Overidden variables