wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes explicit  casting 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 "explicit  casting" Watch "explicit  casting" New topic
Author

explicit casting

Roopa Bagur
Ranch Hand

Joined: Nov 03, 2000
Posts: 267
Can somebody expalin to me why we need explicit casting at line 25?
1: class Test
2: {
3: static void show()
4: {
5: System.out.println("Show method in Test class");
6: }
7: }
8:
9: public class Q2 extends Test
10: {
11: static void show()
12: {
13: System.out.println("Show method in Q2 class");
14: }
15: public static void main(String[] args)
16: {
17: Test t = new Test();
18: t.show();
19: Q2 q = new Q2();
20: q.show();
21:
22: t = q;
23: t.show();
24:
25: q = t;
26: q.show();
27: }
28: }
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
it's because Q2 is a subclass of Test. You may assign an instance of class Q2 to a variable of type Test (as on line 22) since the conversion is up the hierarchy. But if you want to assign "down" the hierarchy a cast is needed. The cast tells the compiler that you know what you are doing. Remember that a variable of type superclass may reference any instance the type subclass, but not the opposite.
For instance, you have a hierarchy like this

where A is the superclass and B and C are the subclasses of A but are not related to each other. Look at the following code:

As you can see a and a1 are variable of type A. a points to an instance of B which is legal and a1 points to an instance of C which is legal too. BUT, if you don't provide a cast on the last line it means that you are trying to assign an instance of class C to a variable of type B which is not legal since B and C are not related by any way...
In your example, you have to write line 25 as follows
q = (Q2) t;
and this line tells the compiler that you are sure that the variable t will be holding at runtime an instance of class Q2 which is the case according to line 22.
I don't know if this is clear enough but let me know if not...
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
Desai Sandeep
Ranch Hand

Joined: Apr 02, 2001
Posts: 1157
Hi Roopa,
Remember that you need a explicit cast if you are pointing a subclass reference to a superclass reference(which should hold a subclass object to run correctly!)
In this case, superclass is Test and subclass is Q2.So if you have an object of Q2 being stored in superclass reference you donot require explicit cast.
For example :

In the following case, you require an explicit cast!

Note that in the above case the subclass reference is pointing to superclass reference, which holds a subclass object.
HTH,
Sandeep
SCJP2, OCSD(Oracle JDeveloper), OCED(Oracle Internet Platform)
[This message has been edited by Desai Sandeep (edited October 22, 2001).]
Roopa Bagur
Ranch Hand

Joined: Nov 03, 2000
Posts: 267
Good explanation Val & Sandeep.. I am clear about this now..
Thanks,
Roopa.
Originally posted by Valentin Crettaz:
[B]it's because Q2 is a subclass of Test. You may assign an instance of class Q2 to a variable of type Test (as on line 22) since the conversion is up the hierarchy. But if you want to assign "down" the hierarchy a cast is needed. The cast tells the compiler that you know what you are doing. Remember that a variable of type superclass may reference any instance the type subclass, but not the opposite.
For instance, you have a hierarchy like this

where A is the superclass and B and C are the subclasses of A but are not related to each other. Look at the following code:

As you can see a and a1 are variable of type A. a points to an instance of B which is legal and a1 points to an instance of C which is legal too. BUT, if you don't provide a cast on the last line it means that you are trying to assign an instance of class C to a variable of type B which is not legal since B and C are not related by any way...
In your example, you have to write line 25 as follows
q = (Q2) t;
and this line tells the compiler that you are sure that the variable t will be holding at runtime an instance of class Q2 which is the case according to line 22.
I don't know if this is clear enough but let me know if not...
HIH

[/B]

Yin Ming
Ranch Hand

Joined: Oct 12, 2001
Posts: 41
first the conversion from superclass to subclass is a norrowing conversion. so you must use case explicitly.
but these code is a bad one. it seems explaining the polymorphism, but its not polymorphism. the both show methods are static, they are class methods. their execution depends on the variable's compile-time type, the type in the definition. so regardless which the instance's runtime type is, the actual executed method is the one of the variable's type.
hope my explaining clearly
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: explicit casting