0

How do you convert objects of one class to objects of another class? For example, let's say Object A has the attributes "height" and "weight." How do I check to see if Object B has these same attributes? And if Object B does have similar attributes as Object A, how do I import those values to Object A and/or create a new object with those attributes and values?

Thanks for your help!

2
  • 1
    Question is too broad Commented Feb 12, 2015 at 19:35
  • 2
    This is impossible in Java as it is a strict type language. The way around is to define Name-Value pair class and then store different pairs in a collection. You can transfer elements between different collections easily in Java. Commented Feb 12, 2015 at 19:40

4 Answers 4

1

There is no single answer for all cirumstances. Some of the more common cases are:

  • Two classes with the same purpose, but from different libraries. Each library works only with its own type. You need to write th glue code that build an instance of A for each B and handle conversion yourself (with explicitly written code).

  • Two classes with the same purpose, but you have control over one of the classes. Eliminate that class and only use the other.

  • Two classes but not the same purpose, only a common aspect. If you have control over both of them. Use either inheritance (if one is a special case of the other), delegation (the two classes are unrelated, but have a common aspect can be factored out) or define an interface with the common aspect and have both classes implement it.

Sign up to request clarification or add additional context in comments.

Comments

1

You can use Commons-BeanUtils BeanUtilsBean, the method copyProperties

http://commons.apache.org/proper/commons-beanutils/apidocs/org/apache/commons/beanutils/BeanUtilsBean.html

Code :

BeanUtilsBean bub= BeanUtilsBean.getinstance();

bub.copyProperties(yourObject1Dest,yourObjectOrig);

Comments

0

If I get your question :

public class A{
int height, weight;

  public A(int height, int weight){
    this.height = height;
    this.weight = weight;
  }

  public A(A originalObject){
    height = originalObject.height;
    weight = originalObject.weight;
  }
} 

public class B extends A{
  public B(int height, int weight){
    super(height, weight);
  }

  public B(B originalObject){
    super (originalObject);
  }

  public static void main(String [] args){
    B bObject = new B(10,20);
    A aObject = new A(bObject);
  }
}

Comments

-1

I would suggest looking to inheritance.

One way to check if an object has certain attributes is to just start typing out what you want in the IDE, for example:

Object.height

right after typing the "." the elements of that class with show up.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.