0
        public class A
    {

      public void printA(){
        System.out.println("A");  
      }

    }


    public class B extends A
    {
       public void printB(){
        System.out.println("B");  
      }

    }

    public class C extends B
    {
       public void printC(){
        System.out.println("C");  
      }

    }

    public class test {
        public static void main(String[] args)
      {

     A  a = new B();
          a.printA(); // work

        B b = (B) a;
          b.printB(); // work

          C c = (C) b;
          c.printC(); // not work throw  java.lang.ClassCastException

      }
}

i have three classes A and B and C
C extends from B and B extends from A
why down casting work from A to B, and does not work from B to C ,although the relation between them like A and B , B is parent of C so how it work JVM??

  • 1
    You can't change the class of an Object, you can only change the class of a reference. – Peter Lawrey Oct 17 '16 at 10:53
  • i don't understand where i'm trying to change class of object in my code?? – Android AL-Khatib Oct 17 '16 at 11:00
  • When you do `(C) b` you are trying to pretend this changes the type of the object to a `C` but it doesn't. The type of the Object is always a `B`. – Peter Lawrey Oct 17 '16 at 11:04
  • but B is parent of C it's true ? so C is instanceof B and can i do casting!! – Android AL-Khatib Oct 17 '16 at 11:07
  • 1
    This is explained in many places in the internet already. In all respect for stackoverflowers, I doubt that anything that will be written in this thread will be clearer than what is already out there. Please do your research and study the resources. – Ole V.V. Oct 17 '16 at 11:11
  • This is the way you want it to be in object-oriented programming. If A is plant, B is tree and C is oak tree, you have created a tree, and it can be held in a variable of type plant (because a tree is a plant) and in a tree variable (obviously), and it can be assigned back and forth between those two varables. It cannot be held in a variable of type oak tree because it is not an oak tree. – Ole V.V. Oct 17 '16 at 11:14
  • @AndroidAL-Khatib a B object is not an instance of a C so you can't cast it to C because B is not a C. – Peter Lawrey Oct 17 '16 at 11:33
  • I would expect you get the `ClassCastException` already when you do the cast, that is in the line `C c = (C) b;`, and even without calling `printC()`. Isn’t this the case? Your stack trace should tell. – Ole V.V. Oct 17 '16 at 12:13

1 Answers1

6

Classes can only be cast to their parent classes, they have no knowledge about their subclasses.

Since your object is an instance of B, it does not implement methods of C.

This will work:

  A  a = new C();
  a.printA(); // work

  B b = (B) a;
  b.printB(); // work

  C c = (C) b;
  c.printC(); // work 
Michael Kohl
  • 66,324
  • 14
  • 138
  • 158
maxpovver
  • 1,580
  • 14
  • 25
  • You can cast class only to it's parent, but not to it's child. Because class doesn't know anything about sublasses. – maxpovver Oct 17 '16 at 11:11