Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: About casting

  1. #1
    Junior Member
    Join Date
    Oct 2013
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default About casting

    Why can my code be compiled ? And why can't J_SuperClass be cast to J_SubClass ?
    result.jpg
    The following is my code:
    class J_SuperClass {
    }
     
    class J_SubClass extends J_SuperClass {
    }
     
    public class J_Test {
    	public static void main(String[] args) {
    		J_SuperClass a = new J_SuperClass();
    		J_SubClass b = new J_SubClass();
    		b = (J_SubClass)a;
    	}
    }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: About casting

    Search and read up on "java downcasting".

  3. The Following User Says Thank You to GregBrannon For This Useful Post:

    CheukKwan (October 18th, 2013)

  4. #3
    Junior Member
    Join Date
    Oct 2013
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: About casting

    I have read up the concept of what you said. However, I want to know if 'a' can reference a J_SubClass or not. And why ? That's the key.

  5. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: About casting

    If you mean, "Can a use methods of the subclass?" the answer is "No."

  6. #5

    Default Re: About casting

    Quote Originally Posted by CheukKwan View Post
    I have read up the concept of what you said. However, I want to know if 'a' can reference a J_SubClass or not. And why ? That's the key.
    Of course 'a' can reference a J_SubClass which is a subtype of J_SuperClass which is declared type of 'a'.

  7. #6
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: About casting

    Because inheritance goes one way.
    J_SubClass references J_SuperClass because J_SubClass extends J_SuperClass. However, J_SuperClass is not even aware that J_SubClass exists to begin with.
    A super class does not know, or care, what classes extend from it.
    All J_SubClasses can be casted into J_SuperClasses, but it doesn't go the other way. J_SuperClass cannot be casted into a J_SubClass.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  8. The Following User Says Thank You to aussiemcgr For This Useful Post:

    CheukKwan (October 18th, 2013)

Similar Threads

  1. Casting from a whole number to a percent
    By ///M Dood in forum Java Theory & Questions
    Replies: 2
    Last Post: September 22nd, 2013, 05:15 PM
  2. Explicit Casting?
    By syregnar86 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 14th, 2013, 06:44 PM
  3. Casting Theory
    By BigDru in forum Java Theory & Questions
    Replies: 1
    Last Post: October 25th, 2012, 08:21 AM
  4. Class Casting
    By Sana1990 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 9th, 2012, 08:13 AM
  5. Type casting error in Java
    By Eric in forum Java Theory & Questions
    Replies: 3
    Last Post: December 13th, 2008, 04:11 PM