Extending a Bean? ClassCastException

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Casper B

    Extending a Bean? ClassCastException

    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
    ClassCastExcept ion. Is it not possible to cast in this direction?


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

    public setStrMemeber(S tring str)
    {
    strMember = str;
    }

    public getStrMemeber()
    {
    return strMember;
    }
    }


    public class MyObject extends MyObjectBean
    {
    public void print()
    {
    System.out.prin tln(this.getStr Member());
    }
    }


    ....
    MyObjectBean myobjectbean = new MyObjectBean();
    MyObject mo = ( (MyObject)myobj ectbean ).print(); << ClassCastExcept ion
    ....

    Thanks in advance,
    Casper
  • Tjerk Wolterink

    #2
    Re: Extending a Bean? ClassCastExcept ion

    Casper B wrote:[color=blue]
    > 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
    > ClassCastExcept ion. Is it not possible to cast in this direction?
    >
    >
    > public class MyObjectBean
    > {
    > publc MyObjectBean(){ }
    > private String strMember;
    >
    > public setStrMemeber(S tring str)
    > {
    > strMember = str;
    > }
    >
    > public getStrMemeber()
    > {
    > return strMember;
    > }
    > }
    >
    >
    > public class MyObject extends MyObjectBean
    > {
    > public void print()
    > {
    > System.out.prin tln(this.getStr Member());
    > }
    > }
    >
    >
    > ...
    > MyObjectBean myobjectbean = new MyObjectBean();
    > MyObject mo = ( (MyObject)myobj ectbean ).print(); << ClassCastExcept ion[/color]

    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=(RaceCa r)ford;

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

    [color=blue]
    > ...
    >
    > Thanks in advance,
    > Casper[/color]

    Comment

    • news.tele.dk

      #3
      Re: Extending a Bean? ClassCastExcept ion

      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 -> BeanWithBusines sMethods

      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

      Comment

      • Raymond DeCampo

        #4
        Re: Extending a Bean? ClassCastExcept ion

        news.tele.dk wrote:[color=blue]
        > 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 -> BeanWithBusines sMethods
        >
        > 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?
        >[/color]

        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.

        Comment

        • Dan Nuttle

          #5
          Re: Extending a Bean? ClassCastExcept ion

          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" <casper@jbr.d k> wrote in message
          news:423044f4$0 $255$edfadb0f@d read12.news.tel e.dk...[color=blue]
          > 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
          > ClassCastExcept ion. Is it not possible to cast in this direction?
          >
          >
          > public class MyObjectBean
          > {
          > publc MyObjectBean(){ }
          > private String strMember;
          >
          > public setStrMemeber(S tring str)
          > {
          > strMember = str;
          > }
          >
          > public getStrMemeber()
          > {
          > return strMember;
          > }
          > }
          >
          >
          > public class MyObject extends MyObjectBean
          > {
          > public void print()
          > {
          > System.out.prin tln(this.getStr Member());
          > }
          > }
          >
          >
          > ...
          > MyObjectBean myobjectbean = new MyObjectBean();
          > MyObject mo = ( (MyObject)myobj ectbean ).print(); << ClassCastExcept ion
          > ...
          >
          > Thanks in advance,
          > Casper[/color]


          Comment

          • news.tele.dk

            #6
            Re: Extending a Bean? ClassCastExcept ion

            Raymond DeCampo wrote:[color=blue]
            > 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[/color]

            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

            Comment

            Working...