aspose file tools*
The moose likes Beginning Java and the fly likes Class Cast problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Class Cast problem" Watch "Class Cast problem" New topic
Author

Class Cast problem

Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 437
Hi,
The problem is:
class A{
}
class B extends A{
public static void main(String args[])
{
A a=new A();
B b=new B();
b=(B)a; // this line
}
}
This line is giving class cast exception.. can someone explain why?


Help gets you when you need it!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

A cast tells the compiler that although the type of a variable is of some general type, the object the variable refers to is actually of some more specific type. This is checked at runtime, and if it's wrong, you get a ClassCastException.
In your case, the variable "a" is pointing to an instance of class A, but your cast is claiming that's it's actually an instance of B. It's not, of course; at run time, the foul deception is revealed and the exception is thrown.


[Jess in Action][AskingGoodQuestions]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Of course you could use a=(A)b; without such an error.
I'm thinking that you might enjoy the "How my Dog learned Polymorphism" article in The JavaRanch Campfire Stories.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 437
Thank you guys for swift reply
One can always use
a=b; instead of a=(A)b; too.
But the main problem would be to use a method in class A eg:
public void printMe(){
}
in class B
eg
B b = new B();
b.printMe();
So how to use it...
Maki Jav
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

If "printMe()" is defined in A, and B extends A, then you can just call the method (unless, of course, access restrictions don't allow you to -- i.e., the method is private). That's an important part of what inheritance is all about. a B "is-a" A; anything you can do to an A, you can do to a B.
Siva kandasamy
Ranch Hand

Joined: Dec 31, 2002
Posts: 139
Hi there,
> a=b; instead of a=(A)b; too.
It Looks like casting is an automatic. ie a=b.
Why do we have the logic to do the casting like "a=(A)b;"
When and where, you have to do the casting, very explicitly ?
thnaks
siva
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

All B's are A's since B extends A, so no cast is necessary when assigning a value of type B to a variable of type A. Some A's are B's, and some aren't, so when assigning a value of type A to a variable of type B, the cast is needed to tell the compiler that that particular A is, in fact, a B.
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 437
Thanks again guys!
Well! now the relative tougher questions!
The question for the time-being is:
class A{
// class A does not have the method printMe()
}
class B extends A{
public void printMe(){
System.out.println("I am B");
}
}
class C extends A{
public void printMe(){
System.out.println("I am C");
}
}
In class Test we want to call printMe() of class B and class C. Now
how to use polymorphism so that
a.printMe();
compiles with without error.
Maki Jav
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

If the variable "a" is of type A, then of course this can't compile, since it makes no sense to call printMe() on an A object. The best you could do would be
((B) a).printMe();
i.e., cast "a" to a B (which you can only do if "a" is pointing to a B or C object) and then call printMe() on the result.
Polymorphism really has nothing to do with this; that term refers to invoking different implementations of a single method within an inheritance hierarchy. If the root class doesn't have a given method, then that method can't be called, polymorphically or otherwise, on base class objects.
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 437
Thanks for the above reply. Now the second question is ?
Is it better to use interface or a class. After all the above discussion,
I recall my teacher telling me almost a year back that when programming think in terms of interfaces and not of classes. ie make interfaces first and only after that do what you want to with the child classes.
what you say?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Yes, I agree with that statement. More specifically, when writing a method that takes an argument of some reference (non-primitive) type, consider whether that type can be an interface rather than a class. The main reason is flexibility; let me explain.
Imagine you've got a class Foo which has seven different member variables -- it's a fairly big object. You've also got a method process() to which you want to pass Foo objects as an argument. process(), in turn, calls Foo.getA() and Foo.getB(). Now, how do you define process()? You could write it as
void process(Foo f) { ... }
Now, next week, you realize that some Foo's need only two of the seven member variables, so they can be smaller. A SmallFoo class would be nice. Can you pass SmallFoo objects to process()? No, you can't, not unless SmallFoo extends Foo (which obviously defeats the purpose!)
If instead you had written process() to take a IFoo as an argument, where IFoo is an interface with a getA() and a getB() method, then both Foo and SmallFoo could implement it, and both could be passed to process().
You might think this is a fairly abstract argument, but trust me, I learned this the hard way -- it really happens.
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 437
Hi Ernest,
Thanks for your reply. I have not come across such a situation as yet where I have to begin from inheritances but I have a feeling that I will have to take the same road that others have taken. I am lucky that I have a more clearer concept of it all than I had at the begining of this thread!
Maki Jav
Ranch Hand

Joined: May 09, 2002
Posts: 437
Hi Dirk,
The article on polymorphism was really nice!
Maki Jav
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Class Cast problem