Results 1 to 8 of 8
  1. #1
    einnhann is offline Member
    Join Date
    Dec 2009
    Posts
    6
    Rep Power
    0

    Default Wrap an object into another object type

    Now I got 2 objects which from different classes ClassA and ClassB but they have exactly same attributes. ClassA and ClassB is not interface. Assume that ClassB can not be modify.
    Can wrapper wrap classB into classA? For what I know wrapper can wrap primitive class into object type. As for reflection can retireve classB field and set it to classA but due to the security concern, can wrapper do that?

    public ClassA retrieveDetails(int id) {
    ClassA classA = null;
    ClassB classB = null;

    // Error occur here classB.retrieveDetails(id) this method return ClassB instead of ClassA
    classA = classB.retrieveDetails(id);

    return classA;
    }

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by einnhann View Post
    Now I got 2 objects which from different classes ClassA and ClassB but they have exactly same attributes. ..
    Why then do you have these two different classes?

  3. #3
    einnhann is offline Member
    Join Date
    Dec 2009
    Posts
    6
    Rep Power
    0

    Default

    Ya for some reason must have this 2 classes.

  4. #4
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    if ClassB is a subclass of ClassA, then yes.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default

    Quote Originally Posted by einnhann View Post
    for some reason

    Please, it's not my intention to offend you, but that won't do.

    When r\d*x asks why you want to do such a thing, that's not to be nosey. It's because he or she wants to help: but, like the rest of us, can't really help unless they know what it is that you are trying to do. There is a chance that you are going about whatever it is you are doing the wrong way. Rather than "help" you do it wrongly it's only natural that you would be asked what your aim is in doing this.

    And "for some reason" just isn't an answer.

  6. #6
    einnhann is offline Member
    Join Date
    Dec 2009
    Posts
    6
    Rep Power
    0

    Default

    Thanks, Lil_Aziz1 since classB must be subclass of classA i think wrapper can not solve my problem. Thanks for your answer.

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,776
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by einnhann View Post
    Thanks, Lil_Aziz1 since classB must be subclass of classA i think wrapper can not solve my problem. Thanks for your answer.
    This shows once more: code to the interface, not to the implementation; because your client code depends on ClassA or ClassB you are stuck, i.e. you can't give them a wrapper that necessarily has to be another class and it can't extend both from ClassA and ClassB.

    kind regards,

    Jos

  8. #8
    adz
    adz is offline Member
    Join Date
    Jul 2009
    Posts
    35
    Rep Power
    0

    Default

    Sounds like you have a design flaw to me (and everyone else who is wondering why you have two classes that do the same thing)...

Similar Threads

  1. using instanceof to get Object type and parent type?
    By xcallmejudasx in forum New To Java
    Replies: 2
    Last Post: 11-06-2008, 07:24 PM
  2. List views, a type of object
    By Leprechaun in forum New To Java
    Replies: 2
    Last Post: 02-06-2008, 04:07 AM
  3. Need help with creating array of type object
    By riz618 in forum New To Java
    Replies: 3
    Last Post: 01-29-2008, 07:14 AM
  4. Getting object type
    By gapper in forum New To Java
    Replies: 1
    Last Post: 01-20-2008, 09:49 AM
  5. Creating object of Type Object class
    By venkatv in forum New To Java
    Replies: 3
    Last Post: 07-17-2007, 04:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •