Class Casting.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pjerald
    New Member
    • Oct 2007
    • 77

    Class Casting.

    What is the restrictions in castng...
    Inthe following prog if i uncomment c = p.
    It throws,

    Casting.java:28 : incompatible types
    found : test.Parent
    required: test.Child

    is it possible to cast both up and down the hierarchy ?

    Code:
    package test;
    
    class Parent {
    	public static void print(){
    	System.out.println("Parent");
    	}
    }
    
    class Child extends Parent {
    	public static void print()
    	{
    		System.out.print("Child");
    	}
    }
    
    class GrandChild extends Child {
    	public static void print()
    	{
    		System.out.print("GrandChild");
    	}
    }
    public class Casting {
    	public static void main(String args[]){
    		Parent p = new Parent();
    		Child c = new Child();
    		GrandChild g = new GrandChild();
    		p = c;
    //		c = p;
    		System.out.println("Compiles Good");		
    	}
    }
    Thanks,
    P.Jerald
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by pjerald
    What is the restrictions in castng...
    Inthe following prog if i uncomment c = p.
    It throws,

    Casting.java:28 : incompatible types
    found : test.Parent
    required: test.Child

    is it possible to cast both up and down the hierarchy ?

    Code:
    package test;
    
    class Parent {
    	public static void print(){
    	System.out.println("Parent");
    	}
    }
    
    class Child extends Parent {
    	public static void print()
    	{
    		System.out.print("Child");
    	}
    }
    
    class GrandChild extends Child {
    	public static void print()
    	{
    		System.out.print("GrandChild");
    	}
    }
    public class Casting {
    	public static void main(String args[]){
    		Parent p = new Parent();
    		Child c = new Child();
    		GrandChild g = new GrandChild();
    		p = c;
    //		c = p;
    		System.out.println("Compiles Good");		
    	}
    }
    Thanks,
    P.Jerald
    No you cannot cast both up and down the inheritance hierarchy.

    I'm sure you can play around on the compiler to find out which casts are allowed. Also take note of the difference between casts that fail at compile time and those that fail at run time.

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      Originally posted by r035198x
      No you cannot cast both up and down the inheritance hierarchy.
      You can always cast upwards, towards the root which is the Object class. You
      can only cast downwards if the object at hand actually *is an* instance of the
      target class you want to cast to or a subclass thereof. A Parent most certainly
      is not a Child in the example, hence the cast failure.

      kind regards,

      Jos

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by JosAH
        You can always cast upwards, towards the root which is the Object class. You
        can only cast downwards if the object at hand actually *is an* instance of the
        target class you want to cast to or a subclass thereof. A Parent most certainly
        is not a Child in the example, hence the cast failure.

        kind regards,

        Jos
        Yep, I suppose my post ended up all wrong. I wanted to convey that it's not correct to say that "it's always possible to cast both ways" and that the OP is best left to discover those that work and those that do not work ...

        Comment

        • pjerald
          New Member
          • Oct 2007
          • 77

          #5
          Originally posted by JosAH
          A Parent most certainly is not a Child in the example, hence the cast failure.

          kind regards,

          Jos
          Thanks, jos and r035198x.

          Regards,
          P.Jerald.

          Comment

          • pjerald
            New Member
            • Oct 2007
            • 77

            #6
            Originally posted by josAH
            You can always cast upwards, towards the root which is the Object class. You
            can only cast downwards if the object at hand actually *is an* instance of the
            target class you want to cast to or a subclass thereof. A Parent most certainly
            is not a Child in the example, hence the cast failure.


            kind regards,

            Jos


            So, We cannot cast a parent class to its child. Am i wright ?

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by pjerald
              So, We cannot cast a parent class to its child. Am i wright ?
              Yep, true; here's an example:

              [code=java]
              public class MeansOfTranspor tation { ... }
              public class CompactCar extends MeansOfTranspor tation { ... }
              public class Submarine extends MeansOfTranspor tation { ... }
              ...
              Submarine yellow= new Submarine();
              MeansOfTranspor tation mot= (MeansOfTranspo rtation)yellow; // valid up-cast
              CompactCar c= (CompactCar)mot ; // invalid down-cast
              [/code]

              As you can see a submarine clearly isn't a compact car.

              kind regards,

              Jos

              Comment

              • heat84
                New Member
                • Nov 2007
                • 118

                #8
                You need an explicit cast from parent class to child class but no need for explicit class from child to parent to child so if you do this there will be no error

                #
                public static void main(String args[]){
                #
                Parent p = new Parent();
                #
                Child c = new Child();
                #
                GrandChild g = new GrandChild();
                #
                p = c;
                #
                c = (Child)p;
                #
                System.out.prin tln("Compiles Good");
                #
                }

                Comment

                Working...