Results 1 to 7 of 7
  1. #1
    Elaforestis is offline Member
    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0

    Default Need help with Object Casting

    Hey, i'm currently learning Casting and i have been having some difficulties!
    I have 2 classes:
    A superclass called Apple and a subclass called GreenApple:

    GreenApple:
    Java Code:
    public class GreenApple extends Apple {
    GreenApple a = new GreenApple();
    Boolean greenColour;
    a = a2;
    a2 = (Apple) a;
    
    GreenApple() {
    	greenColour = true;
    }
    }
    Apple:
    Java Code:
    public class Apple {
    	public static void main(String[] args) {
    		GreenApple appleColour = new GreenApple();
    		System.out.println(appleColour.greenColour);
    }
    }
    and i get the following compilation errors:

    Exception in thread "main" java.lang.Error: Unresolved compilation problems:
    Syntax error on token ";", { expected after this token
    Syntax error on token "{", ; expected

    at GreenApple.<init>(GreenApple.java:3)
    at Apple.main(Apple.java:4)

    whats wrong?

  2. #2
    steveo314 is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default Re: Need help with Object Casting

    ....
    Last edited by steveo314; 10-17-2012 at 09:55 PM.

  3. #3
    Elaforestis is offline Member
    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0

    Default Re: Need help with Object Casting

    Still getting the same errors

  4. #4
    steveo314 is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default Re: Need help with Object Casting

    I'm playing with your code right now too.
    Yea its nothing with the semi-colon.
    I'm gettin errors on lines 4 & 5 of the GreenApple class about identifiers....

  5. #5
    steveo314 is offline Member
    Join Date
    Oct 2012
    Posts
    7
    Rep Power
    0

    Default Re: Need help with Object Casting

    What are you wanting a and a2 to do?

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

    Default Re: Need help with Object Casting

    Remove lines #2, #4 and #5 from your GreenApple class; at least it will compile then. Never try to run anything when if didn't compile.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Elaforestis is offline Member
    Join Date
    Oct 2012
    Posts
    11
    Rep Power
    0

    Default Re: Need help with Object Casting

    Quote Originally Posted by JosAH View Post
    Remove lines #2, #4 and #5 from your GreenApple class; at least it will compile then. Never try to run anything when if didn't compile.

    kind regards,

    Jos
    Thanks :P
    I was just trying to experiment with object casting in order to further understand its usage.

Similar Threads

  1. Casting a String object to a Form object
    By kakinyim in forum CLDC and MIDP
    Replies: 0
    Last Post: 04-23-2011, 12:41 PM
  2. Object Casting, please help
    By DerekRaimann in forum New To Java
    Replies: 5
    Last Post: 12-04-2010, 02:14 PM
  3. Casting Object to another type
    By green_river48 in forum New To Java
    Replies: 12
    Last Post: 04-03-2010, 11:52 AM
  4. Object casting
    By spiderweb in forum Advanced Java
    Replies: 5
    Last Post: 08-20-2009, 06:43 PM
  5. Object to Int casting
    By nn12 in forum New To Java
    Replies: 4
    Last Post: 12-06-2008, 11:58 PM

Tags for this Thread

Posting Permissions

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