Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2006

Tip: Looking for answers? Try searching our database.

Permanent casting of an object

Thread view: 
Cruella DeVille - 11 May 2006 14:28 GMT
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?
Gordon Beaton - 11 May 2006 14:49 GMT
> I have one abstract class Form in which ApplicationForm and
> KindergartenForm extends. In my Query class I check wether Form f is
[quoted text clipped - 6 lines]
> 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

Signature

[  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

Cruella DeVille - 11 May 2006 17:09 GMT
Thanks! Never would have thought of that on my own

Much better code now!
Robert Klemme - 11 May 2006 16:59 GMT
> I have one abstract class Form in which ApplicationForm and
> KindergartenForm extends. In my Query class I check wether Form f is
[quoted text clipped - 17 lines]
>
> }

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
Chris Uppal - 11 May 2006 17:16 GMT
> 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
Robert Klemme - 12 May 2006 09:16 GMT
>> KindergartenForm kf = (KindergartenForm ) f;
>> f.kinderMethod();
[quoted text clipped - 6 lines]
>     kf.kinderMethod();
>     kf.anotherKinderMethod();

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

    robert


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.