Permanent casting of an object

Discussion in 'Java' started by Cruella DeVille, May 11, 2006.

  1. I have one abstract class Form in which ApplicationForm and
    KindergartenForm extends. In my Query class I check wether Form f is
    instanceof ApplicationForm vs KindergartenForm and cast f thereafter.

    But everytime I call a method from eg. ApplicationForm on f (that is
    already casted to ApplicationForm) I have to cast again. Is there a way
    to permanently cast f to either ApplicationForm or KindergartenForm or
    do I have to cast every time I invoke a method on my f object?

    Is it so that I can cast from children to parents without stress, but
    the other way around is a repetitive task?

    Form form;
    public void register(Form f){
    if(f istanceof KindergartenForm)
    this.form = (KindergartenForm)f;
    else if(f instanceof ApplicationForm)
    this.form = (ApplicationForm)f;
    // do stuff based on type of form

    }

    This does not permanently cast f to correct type, how come?
    Cruella DeVille, May 11, 2006
    #1
    1. Advertising

  2. On 11 May 2006 06:28:23 -0700, Cruella DeVille wrote:
    > I have one abstract class Form in which ApplicationForm and
    > KindergartenForm extends. In my Query class I check wether Form f is
    > instanceof ApplicationForm vs KindergartenForm and cast f
    > thereafter.
    >
    > But everytime I call a method from eg. ApplicationForm on f (that is
    > already casted to ApplicationForm) I have to cast again. Is there a
    > way to permanently cast f to either ApplicationForm or
    > KindergartenForm or do I have to cast every time I invoke a method
    > on my f object?


    [...]

    > This does not permanently cast f to correct type, how come?


    First, the fact that you seem to need more than occasional use of
    instanceof and casting is a strong indication of poor design. For
    example, the "do stuff based on type of form" probably belongs in the
    specific subclasses. Then you'd simply do f.doStuff() and the correct
    method would be invoked.

    Realize that casting does not change the object or its type in any
    way. Casting simply lets you use an Object reference as if it referred
    to an Object of a different type.

    So if you have a reference that looks like this:

    Form f = ...

    Then f is and always will be a Form reference.

    If you know that your object is a KindergartenForm (a subclass of
    Form), then declare a KindergartenForm reference:

    KindergartenForm kf = ...

    If the Form object is in fact a KindergartenForm, you can assign f to
    kf with a cast:

    kf = (KindergartenForm)f;

    After that, you can use kf as a KindergartenForm without additional
    casting.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
    Gordon Beaton, May 11, 2006
    #2
    1. Advertising

  3. Cruella DeVille wrote:
    > I have one abstract class Form in which ApplicationForm and
    > KindergartenForm extends. In my Query class I check wether Form f is
    > instanceof ApplicationForm vs KindergartenForm and cast f thereafter.
    >
    > But everytime I call a method from eg. ApplicationForm on f (that is
    > already casted to ApplicationForm) I have to cast again. Is there a way
    > to permanently cast f to either ApplicationForm or KindergartenForm or
    > do I have to cast every time I invoke a method on my f object?
    >
    > Is it so that I can cast from children to parents without stress, but
    > the other way around is a repetitive task?
    >
    > Form form;
    > public void register(Form f){
    > if(f istanceof KindergartenForm)
    > this.form = (KindergartenForm)f;
    > else if(f instanceof ApplicationForm)
    > this.form = (ApplicationForm)f;
    > // do stuff based on type of form
    >
    > }


    There is no casting needed. Since f is of type Form and "form" is also
    of type form you can simply assign it. If in your method you need to
    apply KindergartenForm operations to f and you want to avoid repeated
    casting you can simply do

    KindergartenForm kf = (KindergartenForm ) f;
    f.kinderMethod();
    f.anotherKinderMethod();

    You probably come from a C++ background where casting usually involves
    creating a new instance. This is not the case with Java. The situation
    is comparable to using C++ pointers and dynmic_cast.


    HTH

    robert
    Robert Klemme, May 11, 2006
    #3
  4. Thanks! Never would have thought of that on my own

    Much better code now!
    Cruella DeVille, May 11, 2006
    #4
  5. Cruella DeVille

    Chris Uppal Guest

    Robert Klemme wrote:

    > KindergartenForm kf = (KindergartenForm ) f;
    > f.kinderMethod();
    > f.anotherKinderMethod();


    Very small typo, but one which might be rather confusing in this context.
    That should read:

    KindergartenForm kf = (KindergartenForm ) f;
    kf.kinderMethod();
    kf.anotherKinderMethod();

    -- chris
    Chris Uppal, May 11, 2006
    #5
  6. Chris Uppal wrote:
    > Robert Klemme wrote:
    >
    >> KindergartenForm kf = (KindergartenForm ) f;
    >> f.kinderMethod();
    >> f.anotherKinderMethod();

    >
    > Very small typo, but one which might be rather confusing in this context.
    > That should read:
    >
    > KindergartenForm kf = (KindergartenForm ) f;
    > kf.kinderMethod();
    > kf.anotherKinderMethod();


    Ooops! Yes, of course! Thanks for catching that!

    robert
    Robert Klemme, May 12, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raghu Raman
    Replies:
    3
    Views:
    491
    gabe garza
    May 7, 2005
  2. =?Utf-8?B?S2VubnkgTS4=?=

    letting fonts permanent on my site

    =?Utf-8?B?S2VubnkgTS4=?=, May 31, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    420
  3. Alan Silver
    Replies:
    1
    Views:
    737
    Alan Silver
    Feb 2, 2006
  4. Gawelek

    Permanent deploy on Tomcat

    Gawelek, Oct 23, 2003, in forum: Java
    Replies:
    0
    Views:
    381
    Gawelek
    Oct 23, 2003
  5. df

    Web Service with permanent object cached?

    df, Dec 6, 2005, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    175
Loading...

Share This Page