Results 1 to 3 of 3
  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default A small question concerning super

    A small question concerning super
    Here is a code sample from the Think in Java book
    why is sup.getField one and not zero (see code all the way down)

    It probably boils down to the difference of
    Super s = new Sub();
    Super s = new Super();
    right?





    Java Code:
    //: polymorphism/FieldAccess.java 
    // Direct field access is determined at compile time. 
     
    class Super { 
      public int field = 0; 
      public int getField() { return field; } 
    } 
     
    class Sub extends Super { 
      public int field = 1; 
      public int getField() { return field; } 
      public int getSuperField() { return super.field; } 
    } 
     
    public class FieldAccess { 
      public static void main(String[] args) { 
        Super sup = new Sub(); // Upcast 
        System.out.println("sup.field = " + sup.field + 
          ", sup.getField() = " + sup.getField()); 
        Sub sub = new Sub(); 
        System.out.println("sub.field = " + 
          sub.field + ", sub.getField() = " + 
          sub.getField() + 
          ", sub.getSuperField() = " + 
          sub.getSuperField()); 
      } 
    } /* Output: 
    sup.field = 0, sup.getField() = 1
    sub.field = 1, sub.getField() = 1, sub.getSuperField() = 0 
    *///:~
    Last edited by willemjav; 02-08-2013 at 07:44 PM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    4,017
    Rep Power
    10

    Default Re: A small question concerning super

    Why do you think it should be 0?

    The instance is of Sub type. The Sub getField() method returns the field value of the Sub class. That value is 1.
    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
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: A small question concerning super

    yes i see that now
    i got confused by:
    Super sup = new Sub(); // Upcast

    thanks

Similar Threads

  1. I am a super noob with a super noob question.
    By LittleZoppo in forum Java Applets
    Replies: 3
    Last Post: 04-27-2012, 04:50 AM
  2. Please help,just a small question
    By JavaDreams in forum New To Java
    Replies: 5
    Last Post: 01-28-2011, 09:15 PM
  3. small question about 'this'
    By GPB in forum New To Java
    Replies: 3
    Last Post: 02-28-2010, 06:46 PM
  4. Super simple question
    By jigglywiggly in forum New To Java
    Replies: 3
    Last Post: 01-26-2009, 02:17 AM
  5. A small Question
    By Eku in forum JDBC
    Replies: 7
    Last Post: 09-01-2008, 07:10 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
  •