By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,213 Members | 2,172 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,213 IT Pros & Developers. It's quick & easy.

Extending a Bean? ClassCastException

P: n/a
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
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Casper B wrote:
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
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

Jul 17 '05 #2

P: n/a
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
Jul 17 '05 #3

P: n/a
news.tele.dk wrote:
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?


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

--
XML is the programmer's duct tape.
Jul 17 '05 #4

P: n/a
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.

"Casper B" <ca****@jbr.dk> wrote in message
news:42*********************@dread12.news.tele.dk. ..
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

Jul 17 '05 #5

P: n/a
Raymond DeCampo wrote:
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

Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.