Results 1 to 3 of 3
  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Question on downcast ?

    Java Code:
    public class ClassCasts 
    {
    	public static void main(String args[])
    	{
    		double myDouble = 43;
    		int myInt = (int)myDouble;
    		System.out.println(myInt);
    		
    		Building myBuilding = new Building();
    		House h = (House) myBuilding;
    		
    		System.out.println(b.getClass());
    	}
    }
    Note: Class House extends Class Building and both these classes have nothing in their bodies.

    Why can I cast from a double to an integer, but I can't do the same operation with my own classes ?

    Thank you.

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,310
    Blog Entries
    7
    Rep Power
    20

    Default Re: Question on downcast ?

    A cast on a class instance doesn't change that instance in any way (unlike casts of,say, int <---> double). If a class D extends class B, you can only cast a B object to an object of class D if the object was an object of D already; so you can only cast your Building object to type House if the Building object was a House object already:

    Java Code:
    Building b= new House();
    House d= (House)b; // this works
    b= new Building();
    d= (House)b; // this fails
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Question on downcast ?

    The casting of numeric primitives is allowed and specifically described in the JLS, or Java Language Specification, a document I encourage you to read. Down casting of objects is only allowed if there is a possibility that it might succeed at run-time.

    So this could possibly work:

    Java Code:
    Building myBuilding = createHouse();
    House h = (House) myBuilding;
    But this will never work:
    Java Code:
    Building myBuilding = new Building();
    House h = (House) myBuilding;
    Because Java knows at compile time that you're trying to down-cast a Building object to House, and this is explicitly not allowed by Java per the JLS. For more on why this is bad, please read this tutorial: Up-casting and Down-casting

Similar Threads

  1. Java Question [Beginner Question]
    By joker760 in forum New To Java
    Replies: 3
    Last Post: 12-13-2011, 04:01 PM
  2. question posted by indissa: library question.
    By Fubarable in forum New To Java
    Replies: 2
    Last Post: 11-18-2011, 01:14 AM
  3. Question concerning question marks and colons
    By jim01 in forum New To Java
    Replies: 17
    Last Post: 01-14-2011, 12:05 AM
  4. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 08:49 AM
  5. Question!
    By anjogomez in forum Java Applets
    Replies: 2
    Last Post: 02-21-2009, 03:24 AM

Posting Permissions

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