Downcasting problem

Discussion in 'Java' started by parkarumesh@gmail.com, Mar 17, 2006.

  1. Guest

    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?
    */
    , Mar 17, 2006
    #1
    1. Advertising

  2. Paul Hamaker Guest

    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
    Paul Hamaker, Mar 17, 2006
    #2
    1. Advertising

  3. Guest

    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.................
    , Mar 17, 2006
    #3
  4. Bart Cremers Guest

    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
    Bart Cremers, Mar 17, 2006
    #4
  5. 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
    tom fredriksen, Mar 17, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Sm9l?=

    Upcasting/ Downcasting in VB.NET?

    =?Utf-8?B?Sm9l?=, Nov 14, 2005, in forum: ASP .Net
    Replies:
    3
    Views:
    4,797
    John Murray
    Nov 14, 2005
  2. Stuart Golodetz
    Replies:
    6
    Views:
    427
    Chris \( Val \)
    Aug 30, 2003
  3. Michael
    Replies:
    2
    Views:
    440
    Michael
    Oct 16, 2004
  4. Random

    CType and downcasting classes

    Random, Dec 27, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    446
    Random
    Dec 28, 2006
  5. Nikola Skoric

    downcasting problem

    Nikola Skoric, Oct 25, 2010, in forum: Python
    Replies:
    8
    Views:
    322
    Lawrence D'Oliveiro
    Oct 30, 2010
Loading...

Share This Page