Alex Dowining
Alex Dowining

Reputation: 970

Casting in java, technical info

I'm trying to understand what happend when someone casts an object of one specific class to another class. I.e there are two classes

 public class Aclass{

   private String attribute1;

   private List<String> attribute2;

   //get/set methods

 }

 public class Bclass{

   private String attribute1;

   private List<int> attribute2;

   //get/set methods

 }

Now inside another class i have both these objects and afterwards i cast them.

public class Cclass{

   Aclass aclass=new Aclass();

   //returneddata is a method that returns an Aclass object that contained dta for the Aclass attributes
   aclass=returneddata();

   Bclass bclass=new Bclass();


   bclass=Bclass.class.cast(aclass);

}

From the aforementioned class i'm taking a java.lang.ClassCastException.

The problem is that i have two classes which does not contained only two attributes each but contained both of them 20. The 16 of these atributes are common in both classes.

Moreover the returneddata method returns an object retrieved from a repository. As you can understand i want to find a way and transfer the 16 data attributes of the object Aclass to the object Bclass.

I want:

1) To find a way to rewrite the 16 common attributes data from the one object to the other

2) How the cast is working in general

Any propositions?

Upvotes: 0

Views: 146

Answers (4)

Simeon Visser
Simeon Visser

Reputation: 122376

The fact that both classes have similar attributes does not matter. What matters is that Aclass and Bclass are not related to each other: one is not a superclass of the other. If you want to share or move data between these classes, you'll need to copy the data from one instance to another yourself.

A casting example

Let's say we have an object Fruit fruit = new Banana();. Now if you'd need to call a method of Banana later in the code, you can cast fruit with the cast (Banana). Why? Because you happen to know that fruit is a Banana but the Java compiler does not know that (it thinks it's a Fruit and it could also be a different fruit). So you have to add the cast to add additional information about fruit and to perform the operation safely.

Upvotes: 8

JERiv
JERiv

Reputation: 554

The best solution here would be write super class that contains the common elements of both classes and then have each of your subclasses extend the super class, that way you could have a list of type SuperClass

public class SuperClass{
   protected String att1_common;
   protected List<String> att2_common;
}

import SuperClass
public class Aclass extends SuperClass {
   private String uniqueAtt1; 
   //get/set methods
}

import SuperClass
public class Bclass extends superClass{
   private String uniqueAtt1;
   //get/set methods
}

note the protected scope modifier, this means private except to subclasses

so now:

List<SuperClass> listofstuff;
try{ Aclass item = listofstuff[0];}
catch( java.lang.ClassCastException e){try Bclass item = listofstuff[0];}

Upvotes: 2

biziclop
biziclop

Reputation: 49744

Java classes and interfaces form a DAG based on their relationships defined by the extends or implements keywords.

If A extends B or A implements B, then B is a direct superclass or superinterface of A.
If A is a direct superclass/interface of B and B is likewise to C, then A is an indirect superclass/interface of C.
The java.lang.Object class is the superclass of every class.

An object of class A can only be cast to B if B is a superclass/interface of A (or when A and B are the same class).

There are also some special rules for certain constructs (arrays, primitive types) but the above rules should make it clear why your cast isn't working.

A complete description can be found here.

Upvotes: 2

Kennet
Kennet

Reputation: 5796

The two classes Aclass and Bclass does not have a common super class or interface hence the ClassCastException. In this particular case you could use BeanUtils, http://commons.apache.org/beanutils/, to copy the common properties from Aclass to Bclass.

Upvotes: 2

Related Questions