Results 1 to 4 of 4
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Creating an instance

  1. #1
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Creating an instance

    Good people please help with this:

    say class Apple extends class Fruit, is it allowed to have something like this

    Apple c = new Fruit();

    Just saw it in a mock exam

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

  3. #3
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: Creating an instance

    my bad, my bad! Forgive me,I was just being lazy. I just tried it, & it can't compile because Apple & Fruit are incompatible types

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

    Default Re: Creating an instance

    No, it's not because their incompatible, but rather the example has them reversed. A general type can be assigned a more specific (child) instance, but not the other way around. This is because the child retains all of the states and behaviors of the parent, and can be held by a parent variable, but again, not the other way around.
    Kagiso likes this.

Similar Threads

  1. Replies: 1
    Last Post: 04-21-2012, 06:03 PM
  2. Java Referencing vs creating instance
    By garnaout in forum New To Java
    Replies: 8
    Last Post: 03-16-2012, 03:12 PM
  3. Finding and Creating Instance of Every Class in Package
    By Julian Trust in forum New To Java
    Replies: 5
    Last Post: 06-25-2011, 08:34 AM
  4. Help with creating an instance of a class extension
    By Inferno719 in forum New To Java
    Replies: 19
    Last Post: 05-04-2011, 03:53 AM
  5. Replies: 10
    Last Post: 01-06-2010, 08:33 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
  •