This discussion is archived
10 Replies Latest reply: Feb 14, 2008 1:25 AM by 796440 RSS

Newbie question: ""dynamic"" casting

807603 Newbie
Currently Being Moderated
Hello all,
<br>
I have a quite newbie question. I have this class hierarcy:
<br>
A
|_A1
|_A2
|_A3
|_A4
|_A5
|_.....
<br>
in some part of my code I have this:
<br><br>
if (object1 instanceof A){
if (object1 instanceof A1)      {A1   object2 = (A1) e;}
          if (object1 instanceof A2)      {A2   object2 = (A2) e;}
          if (object1 instanceof A3)      {A3   object2 = (A3) e;}
          if (object1 instanceof A4)      {A4   object2 = (A4) e;}
          if (object1 instanceof A5)      {A5   object2 = (A5) e;}
.
.
.
object2.callMethod();
}
<br><br>

Is there any way to do this type of casting just in one line? I mean, I just want to cast object1 to the class it is instanceof. If it is instance of A1, I want to be casted to A1, if it is A2 to A2, etc...
<br><br>
Thanks you in advance.
  • 1. Re: Newbie question: ""dynamic"" casting
    800322 Newbie
    Currently Being Moderated
    Rene's First Law of Anti-Static: any idea that features the word "dynamically" can be safely discarded as pointless.


    So if you could cast it into a "dynamic type" <X> - what do you think you could do with it? The compiler wouldn't be able to link any method calls ot access to any attributes or find typos, because it doesn't know what <X> is or has...

    Make use of the overriding mechanism, and/or don't do something with the objects, but rather let the objects to the work themselves, because they know what they are.
  • 2. Re: Newbie question: ""dynamic"" casting
    800322 Newbie
    Currently Being Moderated
    Example: if I tell you "I'll bring you a delivery tomorrow" - how can you plan what you'll do with it? You don't even know what he delivery contains - it could be a pizza, a car, a drink, a letter... You can only plan using the attributes (methods) that all those objects might share. A mutual interface would define these. But you don't even provide this information...
  • 3. Re: Newbie question: ""dynamic"" casting
    807603 Newbie
    Currently Being Moderated
    And why exactly do you need this cast?
  • 4. Re: Newbie question: ""dynamic"" casting
    807603 Newbie
    Currently Being Moderated
    Unfortunatelly I cannot understand very well what you mean. I'm quite new with this language.
    <br><br>
    I'm dealing with that code posted before. How could I call the A1.method, A2.method, A3.method, etc... just knowing if the object1 is instance of A1, A2, A3 etc?
    <br><br>
    Thanks again
  • 5. Re: Newbie question: ""dynamic"" casting
    800322 Newbie
    Currently Being Moderated
    kamikaze04 wrote:
    I'm dealing with that code posted before. How could I call the A1.method, A2.method, A3.method, etc... just knowing if the object1 is instance of A1, A2, A3 etc?
    You don't know what type object1 is of, that's the whole point. For all you know, it's just an A. If you can't guarantee that it has a methodA1 as you would by casting its reference to A1, you can't call that method. It's as simple as that.

    And it still looks to me like you should look up the terms "overriding" and "polymorphism".
  • 6. Re: Newbie question: ""dynamic"" casting
    800322 Newbie
    Currently Being Moderated
    Or in short: not possible. Not even theoretically.
  • 7. Re: Newbie question: ""dynamic"" casting
    796440 Guru
    Currently Being Moderated
    Why do you want to cast it? If you're going to call different methods on it if it's an A1 vs. an A2, then why are you treating it as an A in the first place?
  • 8. Re: Newbie question: ""dynamic"" casting
    807603 Newbie
    Currently Being Moderated
    In fact I know what object1 is on execution time, because the code posted at the top is working well, i just want to avoid repeating that if's for all the new classes Ax I will create.

    In other words if i had from A1 to A200 i dont want to have 200 if's to cast it to the class it is and then execute it's method.

    @CeciNEstPasUnProgrammeur: I will look again the polymorphism chapters and so on to try to learn more, but i think it have it more or less clear. Thanks again.
  • 9. Re: Newbie question: ""dynamic"" casting
    800322 Newbie
    Currently Being Moderated
    kamikaze04 wrote:
    In fact I know what object1 is on execution time,
    Which doesn't help your compiler at all, when it's task to link and verify method calls.
    because the code posted at the top is working well, i just want to avoid repeating that if's for all the new classes Ax I will create.
    Big "code smell" here.
    In other words if i had from A1 to A200 i dont want to have 200 if's to cast it to the class it is and then execute it's method.
    You could call the method "doMagic()" and make it abstract in A. Then you can implement it in all Ax classes and would never have to worry about casting in the first place, because A.doMagic() would automagically do the right thing. Polymorphism.
  • 10. Re: Newbie question: ""dynamic"" casting
    807603 Newbie
    Currently Being Moderated
    You could call the method "doMagic()" and make it abstract in A. Then you can implement it in all Ax classes and would never have to worry about casting in the first place, because A.doMagic() would automagically do the right thing. Polymorphism.
    <br><br>
    Thank you very much...abstract method is exactly what I needed. I'm gonna test it
    <br><br>
    Thank you very much.