Problem with cast and inheritance

F

Franck DARRAS

Hello,

I have two classes
ClassA and ClassB

ClassB extends ClassA

When i creat a new ClassB, I can cast this ClassB to the ClassA
code : ClassA classA = (classA) ClassB;

Problem :
But I would like to create a new instance of ClassA and cast it to the
ClassB, but i obtain a classCastException.

Do you know a solution ?

For the moment, I resolve this problem with this follow code
ClassB classB = new ClassB();
classB.setXXX(ClassA.getXXX);

Thks for your Help

Franck
 
J

Joona I Palaste

Franck DARRAS said:
I have two classes
ClassA and ClassB
ClassB extends ClassA
When i creat a new ClassB, I can cast this ClassB to the ClassA
code : ClassA classA = (classA) ClassB;

No you can't. You have mixed class names and variables around. Try
this instead:
ClassB classB = new ClassB();
ClassA classA = (classA)classB;
but you can just as well use:
ClassA classA = classB;
Problem :
But I would like to create a new instance of ClassA and cast it to the
ClassB, but i obtain a classCastException.

If by "a new instance of ClassA" you mean an instance whose real,
concrete class is ClassB, then you can't cast it to ClassB. An object's
class can never change. Only the types of the variables that refer to
it. Casting *ALWAYS* affects variables - *NEVER* objects.
Do you know a solution ?

I would, if I understood the problem right.
For the moment, I resolve this problem with this follow code
ClassB classB = new ClassB();
classB.setXXX(ClassA.getXXX);

I don't understand this code at all. What are setXXX() and getXXX
supposed to do? Can you post *REAL* code instead of hastily-written
pseudocode?
 
P

Paul H. van Rossem

Hi Franck,

The most obvious reason you can't is that ClassB may have additional
fields. Therefore casting from ClassA to ClassB is undefined, since
these fields don't exist in a ClassA object. You would end up with an
invalid pointer.

Look at at this way: A ClassB object *is a* (specialized) ClassA object,
but a ClassA object *is not* a ClassB object, so assigning it doesn't
make any sense.

Suppose ClassA = Animal and ClassB is Cow (a specialized Animal), then
you can talk about a Cow as being an Animal (therefore Animal anAnimal =
aCow is always valid), but you can't consider an Animal as being a Cow
(it might very well be a Bug instead), unless you "know" it is a Cow,
and then you are allowed to assign Cow aCow = (Cow) anAnimal. But in
this case you have to do it with an explicit cast and you will get a
runtime exception if anAnimal isn't a Cow after all, which can only be
checked at runtime (hence the explicit cast).

Paul.
 
A

Alex Hunsley

Franck said:
Hello,

I have two classes
ClassA and ClassB

ClassB extends ClassA

When i creat a new ClassB, I can cast this ClassB to the ClassA
code : ClassA classA = (classA) ClassB;

Problem :
But I would like to create a new instance of ClassA and cast it to the
ClassB, but i obtain a classCastException.

Do you know a solution ?

You can't do it, because it doesn't make sense to.
A slightly more concrete example: suppose you have an Animal class and a
Mammal class. Mammal extends animal, and so holds a little more data
and/or methods than animal: i.e. there is *more stuff* (a scientific
term) available in the subclass, Mammal.

----------
| Animal | (superclass)
----------
|
| Mammal subclasses
| Animal!
----------
| Mammal | (subclass)
----------

(look, I even wasted 5 minutes drawing a crappy diagram. View in fixed
width font. In retrospect it wasn't worth the bother and now I feel
cheap and used.)

So if you cast an instance of Mammal to Animal, it is ok: you are
effectively masking some of the Mammal instance and making it look like
an Animal. (Note that the actual Mammal object doesn't change; it's just
that the reference to it is an Animal reference.)
Going the other way doesn't make sense. Suppose you have an instance of
Animal and you cast it to Mammal. What is the java runtime supposed to
do if you ask for Mammal specific methods or fields? The information
isn't actually there, so does it just make it up? There is more
information in Mammal than there is in animal, where is it supposed to
come from?

Fundamentally, if you cram a monkey through a cat sized hole, you'll
always have blood on your hands.

alex
 
T

Thomas G. Marshall

Franck DARRAS coughed up:
Hello,

I have two classes
ClassA and ClassB

ClassB extends ClassA

When i creat a new ClassB, I can cast this ClassB to the ClassA
code : ClassA classA = (classA) ClassB;

Problem :
But I would like to create a new instance of ClassA and cast it to the
ClassB, but i obtain a classCastException.

Do you know a solution ?

For the moment, I resolve this problem with this follow code
ClassB classB = new ClassB();
classB.setXXX(ClassA.getXXX);

Thks for your Help

Franck


Listen to all the other posts. If you ever get stuck trying to think it
out, maybe put this somewhere in your head. I'm going to give you things to
remember /without explanation/, so that you can pull it out and use it when
you need it. The other posts give adequate explanations; I'm trying to arm
you with a quick (but obviously incomplete) example.

1.

Memorize: You can always do this:

Object object1 = new Cat();
Object object2 = new Dog();

Object is the mother of all superclasses. It's at the very tippity top.
And because of that, a variable of type Object can hold /any/ other type of
object. That's the first thing to remember. (read the further
explanations elsethread).

Now:

2. Let's say that meow() exists in Cat, and bark() exists in Dog().

Memorize: You /cannot/ do the following:

object1.meow(); // no
object2.bark(); // no

But you /can/ do the following:

((Cat)object1).meow();
((Dog)object2).bark();

Good luck.
 
A

Almeja DT

Don't worry ,
Your Idea is good and you can cast the class the way you want.
The problem is in your code. The correct form to cast your classB is:

ClassA classA = (ClassA) new ClassB();

Try it, it works.
You can also avoid casting by creating an Interface, ex
ClassIDontCareAorBIF and do:

ClassIDontCareAorBIF objectA = new ClassB();
ClassIDontCareAorBIF objectB = new ClassA();

See?
Cheers!
Eddie
 
T

Thomas G. Marshall

Almeja DT coughed up:
Don't worry ,
Your Idea is good and you can cast the class the way you want.
The problem is in your code. The correct form to cast your classB is:

ClassA classA = (ClassA) new ClassB();

....[rip]...

It is perfectly good form to do the following:

ClassA classA = new ClassB();

There is no reason whatsoever to supply the up-cast. It doesn't even make
clearer code.
 
J

Joona I Palaste

Thomas G. Marshall said:
Almeja DT coughed up:
Don't worry ,
Your Idea is good and you can cast the class the way you want.
The problem is in your code. The correct form to cast your classB is:

ClassA classA = (ClassA) new ClassB();
...[rip]...

It is perfectly good form to do the following:
ClassA classA = new ClassB();
There is no reason whatsoever to supply the up-cast. It doesn't even make
clearer code.

Not in this case, but upcasts may be necessary with overloaded methods
or with the ?: operator. I can't think of any other place where they are
needed.
 
J

Jon Martin Solaas

Franck said:
Hello,

I have two classes
ClassA and ClassB

ClassB extends ClassA

When i creat a new ClassB, I can cast this ClassB to the ClassA
code : ClassA classA = (classA) ClassB;

Problem :
But I would like to create a new instance of ClassA and cast it to the
ClassB, but i obtain a classCastException.

Do you know a solution ?

For the moment, I resolve this problem with this follow code
ClassB classB = new ClassB();
classB.setXXX(ClassA.getXXX);

Thks for your Help

Franck

How about this:

public interface IXxx
{
public void setXXX(YYY z);
public YYY getXXX();
}

public class ClassA implements IXxxx
{
...
}

public class ClassB extends ClassA // or implements IXxx
{
...
}

IXxx xxx = new ClassA();

xxx.setXXX(...)
xxx.getXXX();

xxxB = new ClassB();

xxx.setXXX(...);
xxx.getXXX();

I really don't know what you are trying to do, but maybe this gives you
an idea ...
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top