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 / March 2005

Tip: Looking for answers? Try searching our database.

Extending a Bean? ClassCastException

Thread view: 
news.tele.dk - 10 Mar 2005 22:29 GMT
Since I am only able to pass simple beans around using my Web Service
framework, I wonder how to incorporate business logic around these beans.

My idea was to let my Beans be the base class and simply extend these
with more sophisticated classes with the nessesary business logic
inside. However, when I do a cast from MyObjectBean to MyObject I get a
ClassCastException. Is it not possible to cast in this direction?

public class MyObjectBean
{
    publc MyObjectBean(){}
    private String strMember;

    public setStrMemeber(String str)
    {
        strMember = str;
    }

    public getStrMemeber()
    {
        return strMember;
    }
}

public class MyObject extends MyObjectBean
{
    public void print()
    {
        System.out.println(this.getStrMember());
    }
}

...
MyObjectBean myobjectbean = new MyObjectBean();
MyObject mo = ( (MyObject)myobjectbean ).print(); << ClassCastException
...

Thanks in advance,
Casper
klynn47@comcast.net - 10 Mar 2005 22:38 GMT
No, you can't cast in that direction between those object types. The
subclass is a different class because presumably it has added new
things to the class definition. There could be other subclasses of
MyObjectBean that can be referred to as MyObjectBean, but you couldn't
cast those to a MyObject.

The exception would be a method in which the parameter type is a
superclass, and you send an instance of your subclass to it. In the
method it will be referred to as an instance of the superclass but
could be casted to your original subclass because that is the runtime
type.
news.tele.dk - 11 Mar 2005 00:19 GMT
Thanks for your answer,

I think I am confused because if MyObjectBean (implicitly) extends
Object and MyObject extends MyObjectBean, isn't MyObject just a
specilization of MyObjectBean and is then bound to at the very least
have the members that MyObjectBean contains?

My problem is this: I need some mechanism to add methods (perform
operations) on otherwise method-less beans, and I would like if these
operations are member-wise and not some wrapper class which simply
contains the bean as a member:

    JavaBean -> BeanWithBusinessMethods

So I though, why not just enherit from the basic bean and add the
features I need for business processing further down the call-hierachy.
Is there a way to accomplish this?

Kinds regards,
Casper

---
> No, you can't cast in that direction between those object types. The
> subclass is a different class because presumably it has added new
[quoted text clipped - 7 lines]
> could be casted to your original subclass because that is the runtime
> type.
Tor Iver Wilhelmsen - 11 Mar 2005 08:12 GMT
> I think I am confused because if MyObjectBean (implicitly) extends
> Object and MyObject extends MyObjectBean, isn't MyObject just a
> specilization of MyObjectBean and is then bound to at the very least
> have the members that MyObjectBean contains?

Yes. But you are trying to use it the other way around; an instance of
MyObjectBean does not have the behaviour (or fields) introduced in the
subclass.

> My problem is this: I need some mechanism to add methods (perform
> operations) on otherwise method-less beans,

No, you need a class with methods that operate on the beans. Look at
the Visitor pattern.

http://www.dofactory.com/Patterns/PatternVisitor.aspx

Note: You do not need to modify your beans to have an
"accept(Visitor)" method, just call the Visitor's visit*() method
yourself.

> So I though, why not just enherit from the basic bean and add the
> features I need for business processing further down the
> call-hierachy.

But then the _objects_ need to be of the new type and not the original
type.
Casper B - 11 Mar 2005 11:46 GMT
Thanks Tor,

I've only seen the visitor pattern utilized in recursive tree-walking
but indeed it seems to solve my problem as well!

Regards,
Casper


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.