• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

explicit casting

 
Ranch Hand
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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: }
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 1157
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 267
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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]


 
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
snakes are really good at eating slugs. And you wouldn't think it, but so are tiny ads:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic