Results 1 to 5 of 5
  1. #1
    bigo005 is offline Member
    Join Date
    Dec 2013
    Posts
    6
    Rep Power
    0

    Question Type cast in case of classes

    class Test1
    {
    void show()
    {
    System.out.println("Super Class");
    }
    }
    class Test2 extends Test1
    {
    void display()
    {
    System.out.println("Sub Class");
    }
    }
    public class Test
    {
    public static void main(String args [])
    {
    Test1 t1=new Test1();
    Test2 t2=(Test2) t1;
    t2.show();
    t2.display();
    }
    }
    When i run this program it gives me the error Test1 can not be cast to Test2.Why?

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default Re: Type cast in case of classes

    Because that's not how casting works. You can't change the type of an Object, you can only clear up uncertainty. You could do something like this:

    Test1 t1 = new Test2();
    Test2 t2 = (Test2)t1;
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: Type cast in case of classes

    Hmm, actually, you are casting the wrong way.

    You may not cast a super class instance to one if it's subclasses (which is what you are trying to do). You may, however, cast a subclass instance to one of its super classes.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  4. #4
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,874
    Rep Power
    8

    Default Re: Type cast in case of classes

    I'm not sure I agree with you Jim, but I think it might just be your wording. For example, here I'm casing a subclass instance, held in a superclass variable, to that subclass:

    Java Code:
    
    public class Main{
    
    	public static class A{
    		public void print(){
    			System.out.println("A.print()");
    		}
    	}
    
    	public static class B extends A{
    		
    		public void print(){
    			System.out.println("B.print()");
    		}
    
    		public void b(){
    			System.out.println("B");
    		}
    	}
    
    	public static void main(String[] args) {
    		A a = new B();
    		a.print();
    		
    		((B)a).b();
    	}	
    
    }
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,411
    Rep Power
    5

    Default Re: Type cast in case of classes

    Ugh. I mis-read the code but my statement is correct. I said you may not cast a super class instance to one if it's subclasses. You may, however, cast a subclass instance to one of its super classes.


    if Test2 extends Test1

    Test1 t1 = new Test1();
    Test2 t2 = (Test2) t1; // not allowed - violates first sentence.

    Test2 t2 = new Test2();
    Test t1 = (Test1) t2; // allowed - in fact, cast not necessary. complies with second sentence.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Type safety Unchecked cast
    By superhaNds in forum New To Java
    Replies: 9
    Last Post: 11-25-2013, 06:26 PM
  2. Menu Type cast Exception
    By arulmozs in forum AWT / Swing
    Replies: 8
    Last Post: 11-02-2009, 10:34 PM
  3. [SOLVED] Cast string type to int type
    By GilaMonster in forum New To Java
    Replies: 9
    Last Post: 09-17-2008, 10:43 AM
  4. Help determing NamingEnumeration type/cast
    By BlankMan in forum Eclipse
    Replies: 1
    Last Post: 09-05-2008, 07:50 AM
  5. How to cast an Object into a specific type (Integer/String) at runtime
    By mailtogagan@gmail.com in forum Advanced Java
    Replies: 2
    Last Post: 12-03-2007, 01:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •