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

Tip: Looking for answers? Try searching our database.

Extending a Bean? ClassCastException

Thread view: 
Casper B - 10 Mar 2005 13:05 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
Tjerk Wolterink - 10 Mar 2005 23:04 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.
[quoted text clipped - 31 lines]
> MyObjectBean myobjectbean = new MyObjectBean();
> MyObject mo = ( (MyObject)myobjectbean ).print(); << ClassCastException

the variable myobjetbeans is now of the type MyObjectBean ..
its not an instance of MyObjet
so you cant cast

example

RaceCar extends Car extends Vehicle

Car ford=new Car("ford escort");
RaceCar racecar=(RaceCar)ford;

it does not make sense. hope you know what i mean

> ...
>
> Thanks in advance,
> Casper
news.tele.dk - 11 Mar 2005 00:18 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
Raymond DeCampo - 11 Mar 2005 01:36 GMT
> Thanks for your answer,
>
[quoted text clipped - 13 lines]
> features I need for business processing further down the call-hierachy.
> Is there a way to accomplish this?

To accomplish what you are asking in this manner, you need to have
whatever code is creating the MyObjectBean instances, instead create
MyObject instances.

HTH,
Ray

Signature

XML is the programmer's duct tape.

news.tele.dk - 11 Mar 2005 07:24 GMT
> To accomplish what you are asking in this manner, you need to have
> whatever code is creating the MyObjectBean instances, instead create
> MyObject instances.
>
> HTH,
> Ray

Okay, unfortunately that is out of the quesion as the MyObjectBean are
generated automatically by a stub-generator of my WebService toolkit
(Oracle J2EE 1.4 JAX-RPC of JDeveloper).

Thanks for the feedback,
Casper
Dan Nuttle - 11 Mar 2005 01:58 GMT
You can't cast an instance of a subclass as a base class.  Think of it this
way.  You define a class called Animal, and you extend that in a class
called Dog.  You create a Dog variable, and try to set it to an instance of
Animal.  Now, Animal might be any number of animals.  You can't say that a
Dog is a Giraffe.  If Animal has a method called makeSound(), you expect a
Dog to bark, not to...uh...make whatever kind of sound a Giraffe makes.  Or
even whatever kind of sound a generic Animal makes, assuming that you can
instantiate your base class (i.e., that it isn't an abstract class).

Besides, in general you want to define things with the most general type
possible.  Why do you want to declare the variable as a MyObject?  You
should declare it as a MyObjectBean.  (Side note:  Your class naming is kind
of confusing.  But I realize this is probably just for testing purposes.)
Do this:

MyObjectBean bean;
bean = new MyObject();

This is analagous to saying, "I'm creating an Animal.  I want to set it to a
reference to a Dog object."  That's OK...an Animal "is a" Dog.  But a Dog is
not necessarily any arbitrary kind of Animal.

I'm not sure, if you pass a MyObjectBean through web services, if it will be
an instance of a MyObjectBean on the server side, or an instance of
MyObject.  That would have to be tested.  I'm pretty sure that your server
side code would have to be set up to receive a MyObjectBean, since that's
what you're passing...but I don't know whether that it would also be an
instance of a MyObject.  If not, this may be an inherent limitation of web
services.

> 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.
[quoted text clipped - 35 lines]
> Thanks in advance,
> 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.