[newbe] casting at run time

Discussion in 'Java' started by polilop, Jan 11, 2007.

  1. polilop

    polilop Guest

    i have next set of code
    interface A{}
    class B implements A{something}
    clas C implements A{something}

    class D
    {

    public doSomethingWithTypeA(A aClass)

    /***So what i do is***/
    if(B instanceof A)
    cast A to B
    if(C instanceof A)
    cast A to C
    }

    main
    B b=new B();
    D d=new D();
    d.doSomethingWithTypeA(b);

    is it possible to get the class type from A, and then cast to eather B or C
    without checking instanceof (in runtime)?
    Thanks
     
    polilop, Jan 11, 2007
    #1
    1. Advertising

  2. polilop

    Tom Hawtin Guest

    polilop wrote:
    > interface A{}
    > class B implements A{something}
    > clas C implements A{something}
    >
    > class D
    > {
    >
    > public doSomethingWithTypeA(A aClass)
    >
    > /***So what i do is***/
    > if(B instanceof A)
    > cast A to B
    > if(C instanceof A)
    > cast A to C
    >
    > is it possible to get the class type from A, and then cast to eather B or C
    > without checking instanceof (in runtime)?


    It's much better to design your types to use polymorphism, rather than
    attempting to switch on type.

    interface A {
    void doSomething();
    }
    class B implements A {
    public void doSomething() { ...}
    }
    class C implements A {
    public void doSomething() { ...}
    }

    static void doSomethingWithTypeA(A a) {
    a.doSomething();
    }

    If you must check and cast, then the syntax is:

    static void doSomethingWithTypeA(A a) {
    if (a instanceof B) {
    B b = (B)a;
    ...
    } else if (a instanceof C) {
    C c = (C)a;
    ...
    } else {
    throw new Error("Bad design causes errors...");
    }
    }

    Tom Hawtin
     
    Tom Hawtin, Jan 11, 2007
    #2
    1. Advertising

  3. polilop

    Lew Guest

    polilop wrote:
    > class D
    > {
    >
    > public doSomethingWithTypeA(A aClass)
    >
    > /***So what i do is***/
    > if(B instanceof A)
    > cast A to B
    > if(C instanceof A)
    > cast A to C
    > }
    >
    > main
    > B b=new B();
    > D d=new D();
    > d.doSomethingWithTypeA(b);


    Your code as posted will not compile.

    Create a real example. Try it before you post it.

    You cannot invoke the "instanceof" operator with a type as the left operand.

    The variable name "aClass" is misleading since the variable is not of type
    Class but of type A. It is bad practice to embed type information in a
    variable name.

    - Lew
     
    Lew, Jan 12, 2007
    #3
  4. polilop

    polilop Guest

    Sorry for me being hasty, here is the real code

    public interface J2EEVo {
    public abstract boolean isSuccess();
    public abstract void setSuccess(boolean success);
    }
    public class UsersVo implements J2EEVo
    {
    private String USERNAME;
    private String FAMILYNAME;
    private boolean success;

    ... a class constructor and set get methods
    }
    public class ReportVo implements J2EEVo
    {
    private String HEADER;
    private String FOOTER;
    private boolean success;

    ... add class constructor and set get methods
    }


    public class ResultsetToVo {

    public J2EEVo fillVo(J2EEVo uvo,ResultSet rs) throws SQLException
    {
    if(uvo instanceof UsersVo) {
    uvo = (UsersVo)uvo;
    }
    else if(uvo instanceof ReportVo )
    {
    uvo=(ReportVo)uvo;
    }
    }

    To answer Tom Hawtin: I am aware of instanceof, but is there another way to
    cast the uvo at run time, so that i don't have to add a new if (uvo
    insanceof someVo), every time a write a new J2EEVo that needs to use
    ResultsetToVo. Something like (just a thought):

    uvo =(getUvoClassInstance(uvo))uvo
    Thanks


    "Lew" <> wrote in message
    news:...
    > polilop wrote:
    >> class D
    >> {
    >>
    >> public doSomethingWithTypeA(A aClass)
    >>
    >> /***So what i do is***/
    >> if(B instanceof A)
    >> cast A to B
    >> if(C instanceof A)
    >> cast A to C
    >> }
    >>
    >> main
    >> B b=new B();
    >> D d=new D();
    >> d.doSomethingWithTypeA(b);

    >
    > Your code as posted will not compile.
    >
    > Create a real example. Try it before you post it.
    >
    > You cannot invoke the "instanceof" operator with a type as the left
    > operand.
    >
    > The variable name "aClass" is misleading since the variable is not of type
    > Class but of type A. It is bad practice to embed type information in a
    > variable name.
    >
    > - Lew
     
    polilop, Jan 12, 2007
    #4
  5. polilop

    polilop Guest

    Re: [newbe] rewrote my code

    I thought about it, and rewrote my code, so now simpler and works. thanks
    for help
    "Lew" <> wrote in message
    news:...
    > polilop wrote:
    >> class D
    >> {
    >>
    >> public doSomethingWithTypeA(A aClass)
    >>
    >> /***So what i do is***/
    >> if(B instanceof A)
    >> cast A to B
    >> if(C instanceof A)
    >> cast A to C
    >> }
    >>
    >> main
    >> B b=new B();
    >> D d=new D();
    >> d.doSomethingWithTypeA(b);

    >
    > Your code as posted will not compile.
    >
    > Create a real example. Try it before you post it.
    >
    > You cannot invoke the "instanceof" operator with a type as the left
    > operand.
    >
    > The variable name "aClass" is misleading since the variable is not of type
    > Class but of type A. It is bad practice to embed type information in a
    > variable name.
    >
    > - Lew
     
    polilop, Jan 12, 2007
    #5
  6. polilop

    Lew Guest

    Please do not top post.

    polilop wrote:
    > Sorry for me being hasty, here is the real code
    > public class UsersVo implements J2EEVo
    > {
    > private String USERNAME;
    > private String FAMILYNAME;
    > private boolean success;
    >
    > .. a class constructor and set get methods
    > }
    > public class ReportVo implements J2EEVo
    > {
    > private String HEADER;
    > private String FOOTER;
    > private boolean success;
    >
    > .. add class constructor and set get methods
    > }


    By convention, non-constant variable (and method) names should begin with a
    lower-case letter and use camel case. All-upper-case names are reserved for
    static final variables used as class constants.

    > public class ResultsetToVo {
    >
    > public J2EEVo fillVo(J2EEVo uvo,ResultSet rs) throws SQLException
    > {
    > if(uvo instanceof UsersVo) {
    > uvo = (UsersVo)uvo;
    > }
    > else if(uvo instanceof ReportVo )
    > {
    > uvo=(ReportVo)uvo;
    > }
    > }


    The downcasts accomplish exactly nothing. The declared type of uvo will not
    change, and it already knows its own runtime type.

    > To answer Tom Hawtin: I am aware of instanceof, but is there another way to
    > cast the uvo at run time, so that i don't have to add a new if (uvo
    > insanceof someVo), every time a write a new J2EEVo that needs to use
    > ResultsetToVo. Something like (just a thought):


    You might need to rethink the design of fillVo(). Declaring it to take J2EEVo
    arguments implies that it is only interested in the interface behaviors. The
    downcasts tell us that that is a lie, the method really does care about the
    implementation type (aside from the fact that you throw away the result of the
    downcast in your code). You shouldn't take both points of view in the same code.

    You might consider making fillVo() part of the interface, naturally without
    the "uvo" argument. Each overriding class will implement fillVo() knowing full
    well that the implementing type is itself. This is "polypmorphism", which is a
    key concept to good (object-oriented) design.

    A hint that this applies is your explicit use of the uvo argument in
    fill...(), which would be the implicit "this" argument in an instance method.

    Study the idea. You will end up with something similar to:

    public interface J2EEVo {
    public boolean isSuccess();
    public void setSuccess(boolean success);
    public void fill( ResultSet rs );
    }

    - Lew
     
    Lew, Jan 13, 2007
    #6
  7. polilop

    polilop Guest

    "Lew" <> wrote in message
    news:...
    > Please do not top post.
    >
    > polilop wrote:
    >> Sorry for me being hasty, here is the real code
    >> public class UsersVo implements J2EEVo
    >> {
    >> private String USERNAME;
    >> private String FAMILYNAME;
    >> private boolean success;
    >>
    >> .. a class constructor and set get methods
    >> }
    >> public class ReportVo implements J2EEVo
    >> {
    >> private String HEADER;
    >> private String FOOTER;
    >> private boolean success;
    >>
    >> .. add class constructor and set get methods
    >> }

    >
    > By convention, non-constant variable (and method) names should begin with
    > a lower-case letter and use camel case. All-upper-case names are reserved
    > for static final variables used as class constants.
    >
    >> public class ResultsetToVo {
    >>
    >> public J2EEVo fillVo(J2EEVo uvo,ResultSet rs) throws SQLException
    >> {
    >> if(uvo instanceof UsersVo) {
    >> uvo = (UsersVo)uvo;
    >> }
    >> else if(uvo instanceof ReportVo )
    >> {
    >> uvo=(ReportVo)uvo;
    >> }
    >> }

    >
    > The downcasts accomplish exactly nothing. The declared type of uvo will
    > not change, and it already knows its own runtime type.
    >
    >> To answer Tom Hawtin: I am aware of instanceof, but is there another way
    >> to cast the uvo at run time, so that i don't have to add a new if (uvo
    >> insanceof someVo), every time a write a new J2EEVo that needs to use
    >> ResultsetToVo. Something like (just a thought):

    >
    > You might need to rethink the design of fillVo(). Declaring it to take
    > J2EEVo arguments implies that it is only interested in the interface
    > behaviors. The downcasts tell us that that is a lie, the method really
    > does care about the implementation type (aside from the fact that you
    > throw away the result of the downcast in your code). You shouldn't take
    > both points of view in the same code.
    >
    > You might consider making fillVo() part of the interface, naturally
    > without the "uvo" argument. Each overriding class will implement fillVo()
    > knowing full well that the implementing type is itself. This is
    > "polypmorphism", which is a key concept to good (object-oriented) design.
    >
    > A hint that this applies is your explicit use of the uvo argument in
    > fill...(), which would be the implicit "this" argument in an instance
    > method.
    >
    > Study the idea. You will end up with something similar to:
    >
    > public interface J2EEVo {
    > public boolean isSuccess();
    > public void setSuccess(boolean success);
    > public void fill( ResultSet rs );
    > }
    >
    > - Lew

    sorry
     
    polilop, Jan 15, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kevin
    Replies:
    11
    Views:
    5,809
    Andrew McDonagh
    Jan 8, 2005
  2. flamesrock
    Replies:
    8
    Views:
    475
    Hendrik van Rooyen
    Nov 24, 2006
  3. Pierre Yves
    Replies:
    2
    Views:
    489
    Pierre Yves
    Jan 10, 2008
  4. Wally Barnes
    Replies:
    3
    Views:
    528
    Wally Barnes
    Nov 20, 2008
  5. Sosuke

    Up casting and down casting

    Sosuke, Dec 20, 2009, in forum: C++
    Replies:
    2
    Views:
    565
    James Kanze
    Dec 20, 2009
Loading...

Share This Page