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 / First Aid / December 2006

Tip: Looking for answers? Try searching our database.

How to get the instantiated object behind an interface

Thread view: 
bsd_mike - 30 Nov 2006 16:15 GMT
Hello,
Lets say I had the following:

an interface called GMike

Then I have a class:

public void bsd_mike extends Button implements GMike {
}

In my program I have:

GMike mike;
mike = getthemike();
getthemike gives me a bsd_mike object.

I want to be able to do a:

pannel.add(mike);

However the add method in panel does not care for mike being a GMike.

I have created a method in GMike

public Component getSelf()
{ return(this);
}

and then I do:

panel.add(mike.getSelf());

and that works.

Is there a better way though??

Is there an existing method that does what getSelf does?

Thank you,
Mike
Steve W. Jackson - 30 Nov 2006 16:40 GMT
> Hello,
> Lets say I had the following:
[quoted text clipped - 36 lines]
> Thank you,
>  Mike

If your implementing class will always be a subclass of Component,
simply cast the reference to Component when you add it to a container.

= Steve =
Signature

Steve W. Jackson
Montgomery, Alabama

bsd_mike - 30 Nov 2006 18:34 GMT
Thanks.  That works perfect.  I was thinking it should be harder than
that!
-Mike

> > Hello,
> > Lets say I had the following:
[quoted text clipped - 41 lines]
>
> = Steve =
Ingo R. Homann - 01 Dec 2006 09:55 GMT
Hi,

note that such a cast is (in most cases) a hint for a bad design - in
this case a hint that perhaps bsd_mike should not extends Button but
should rather have a method 'getComponent()'. (Note that this is close
to your first solution.)

Ciao,
Ingo
Steve W. Jackson - 01 Dec 2006 17:46 GMT
> Hi,
>
[quoted text clipped - 5 lines]
> Ciao,
> Ingo

Failing to quote relevant portions of a preceding message makes it hard
to grasp what you just said...

I don't freely advocate use of "instanceof" in code, as it often is bad
design.  But in the OP's situation, there was not enough information
about whether his interface would always be used in his code within a
specific kind of class (Component subclasses) or not.  So the end result
of using the getComponent approach is still not entirely clear...what if
his interfact is to be implemented in something besides a Component
subclass?  By virtue of its presence in the interface, he'll be required
to implement the method...but what will he return?

As always, a "correct" reply can only occur with all the facts in hand.  
I have no idea whether he provided all the facts or not.

= Steve =
Signature

Steve W. Jackson
Montgomery, Alabama

Dale King - 02 Dec 2006 01:08 GMT
> Hi,
>
> note that such a cast is (in most cases) a hint for a bad design - in
> this case a hint that perhaps bsd_mike should not extends Button but
> should rather have a method 'getComponent()'. (Note that this is close
> to your first solution.)

Which is also an indication the AWT and Swing should have been designed
with some interfaces that specify the behavior of the underlying
implementations. In reality Component should have probably been an
interface and what is currently Component should be something like
AbstractComponent. Sun was unfortunately very inconsistent on how well
it applied OO design principles.

Signature

 Dale King



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.