• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Class Cast problem

 
Ranch Hand
Posts: 473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Maki Jav
Ranch Hand
Posts: 473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Dirk,
The article on polymorphism was really nice!
Maki Jav
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic