Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Downcasting problem

Reply
Thread Tools

Downcasting problem

 
 
parkarumesh@gmail.com
Guest
Posts: n/a
 
      03-17-2006
I have below example. It compiles fine but gives classcast exception at
run
time. Just curious why Java prohibit down casting.

public class C {
public static void main (String a[]){
B b = (B) new A();
}
}
class A {
public void draw (){
System.out.println("1");
}
public void draw1 (){
System.out.println("2");
}

}
class B extends A {
public void draw (){
System.out.println("3");
}
public void draw2 (){
System.out.println("4");
}
}

/*
also in a class hierarchy which is larger object, the base class or the
derived class? If it is base class, Why?
*/

 
Reply With Quote
 
 
 
 
Paul Hamaker
Guest
Posts: n/a
 
      03-17-2006
B ISA A, but NOT A ISA B, how would you call draw2() if it's
simply not there ?
new A is just that, an A with its limited capabilities, draw() and
draw1() .

--------------------
Paul Hamaker, SEMM, teaching ICT since 1987
http://javalessons.com

 
Reply With Quote
 
 
 
 
parkarumesh@gmail.com
Guest
Posts: n/a
 
      03-17-2006

Paul Hamaker wrote:
> B ISA A, but NOT A ISA B, how would you call draw2() if it's
> simply not there ?
> new A is just that, an A with its limited capabilities, draw() and
> draw1() .
>
> --------------------
> Paul Hamaker, SEMM, teaching ICT since 1987
> http://javalessons.com


Ok fine, just comment out draw2, and even the problem
remains.................

 
Reply With Quote
 
Bart Cremers
Guest
Posts: n/a
 
      03-17-2006
It has nothing to do with the methods available.

B extends A, so B is an A and a B.
A extends nothing (well, Object that is) so A is an A an not a B.

A a = new B(); // Works
B b = (B) a; // Works;
B anotherB = (B) new A(); // Fails

See it like this.

class Shape {}
class Rectangle extends Shape {}
class Circle extends Shape {}
class Triangle extends Shape {}

You can tell that every Rectangle is also a Shape, but not every Shape
is a Rectangle. Some will be Circle and Triangle.

Shape r = new Rectangle(); // Works
Shape c = new Circle(); // Works
Rectangle rect = new Circle(); // Will never compile, because it just
doesn't make sense
Rectangle r = (Rectangle) c;// Will fail at runtime, because c is not a
rectangle.

Regards,

Bart

 
Reply With Quote
 
tom fredriksen
Guest
Posts: n/a
 
      03-17-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Ok fine, just comment out draw2, and even the problem
> remains.................


To expand a bit:

class A consists of only things defined in A.
class B consists of things defined in A and B.
So A can only be A, but B can be both A and B.

In other words "references must be the same or from a super class" for
it to work, which means that the reference can hold any objects of sub
class type. You can not do it the other way around.

If you had done the following, the down casting would have worked:

B b = new B()
A ab = b;
B b2 = (B)ab;

So what you need to do is either do a new B() or redefine it to be an A
reference.

/tom
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
downcasting problem Nikola Skoric Python 8 10-30-2010 02:22 AM
CType and downcasting classes Random ASP .Net 2 12-28-2006 10:16 PM
Upcasting/ Downcasting in VB.NET? =?Utf-8?B?Sm9l?= ASP .Net 3 11-14-2005 08:48 PM
Messy static Downcasting, any alternatives?? Michael C++ 2 10-16-2004 03:28 AM
Multiple and virtual inheritance, and downcasting Stuart Golodetz C++ 6 08-30-2003 02:31 AM



Advertisments