'super...to refer to a variable of the superclass'

Discussion in 'Java' started by Fred, Dec 25, 2010.

  1. Fred

    Fred Guest

    Cadenhead in "Sams Teach Yourself Java 2 in 24 hours" writes that
    'super...can be used...to refer to a variable of the superclass, as in
    super.hawaii = 50.'

    I don't understand why this is in general, possible. If an instance of
    the subclass has been created, it cannot be assumed that an instance
    of the superclass has been simultaneously created. If an instance of
    the superclass has not been created, then the above statement doesn't
    make sense. The assumption is that the hawaii variable is static,
    right?

    And I don't understand why an assignment to a superclass's variable
    would *ever* need to be made *by* the subclass. If the variable is not
    static, the variable has been inherited - playing around with that
    'copy' of the variable should suffice for all purposes, should it not?

    TIA,
    Fred
    Fred, Dec 25, 2010
    #1
    1. Advertising

  2. Fred

    Arne Vajhøj Guest

    On 24-12-2010 19:15, Fred wrote:
    > Cadenhead in "Sams Teach Yourself Java 2 in 24 hours" writes that
    > 'super...can be used...to refer to a variable of the superclass, as in
    > super.hawaii = 50.'
    >
    > I don't understand why this is in general, possible. If an instance of
    > the subclass has been created, it cannot be assumed that an instance
    > of the superclass has been simultaneously created.


    Yes.

    The object created is both an instance of the sub class and the
    super class.

    > If an instance of
    > the superclass has not been created, then the above statement doesn't
    > make sense. The assumption is that the hawaii variable is static,
    > right?


    No.

    > And I don't understand why an assignment to a superclass's variable
    > would *ever* need to be made *by* the subclass. If the variable is not
    > static, the variable has been inherited - playing around with that
    > 'copy' of the variable should suffice for all purposes, should it not?


    Yes - you can simply write:

    hawaii = 50;

    but it is not a 'copy' - it is the real thing.

    You can need to use:
    super.m();
    when you don't want to call the sub class's m method
    (which is often the case if the call is inside
    the sub class's m method).

    Arne
    Arne Vajhøj, Dec 25, 2010
    #2
    1. Advertising

  3. Fred

    Lew Guest

    Fred wrote:
    >> Cadenhead in "Sams Teach Yourself Java 2 in 24 hours" writes that
    >> 'super...can be used...to refer to a variable of the superclass, as in
    >> super.hawaii = 50.'
    >>
    >> I don't understand why this is in general, possible. If an instance of
    >> the subclass has been created, it cannot be assumed that an instance
    >> of the superclass has been simultaneously created.


    Arne Vajhøj wrote:
    > Yes.
    >
    > The object created is both an instance of the sub class and the
    > super class.


    To clarify: it is the same instance that is created, so of course it's
    "simultaneous" because it's the identical object. An object is always created
    at the same time as it itself is created. Thus it is safe to conclude (not
    assume) that an instance of the supertype (it might not be a class) exists.

    Fred wrote:
    >> If an instance of
    >> the superclass has not been created, then the above statement doesn't
    >> make sense. The assumption is that the hawaii variable is static,
    >> right?


    Arne Vajhøj wrote:
    > No.


    Fred wrote:
    >> And I don't understand why an assignment to a superclass's variable
    >> would *ever* need to be made *by* the subclass. If the variable is not


    Because it is the instance's own variable, too.

    (Unless it's private, or package-private from a different package.)

    >> static, the variable has been inherited - playing around with that


    Unless it's private, or package-private from a different package.

    >> 'copy' of the variable should suffice for all purposes, should it not?


    Arne Vajhøj wrote:
    > Yes - you can simply write:
    >
    > hawaii = 50;
    >
    > but it is not a 'copy' - it is the real thing.
    >
    > You can need to use:
    > super.m();
    > when you don't want to call the sub class's m method
    > (which is often the case if the call is inside
    > the sub class's m method).


    Using 'this.' or 'super.' in front of variable names is necessary to
    disambiguate member variables from local variables or hidden or shadowed
    variables, which name conflicts you should avoid anyway.

    Fred, the thing to bear in mind is what Arne mentioned:
    > The object created is both an instance of the sub class [type] and the
    > super class [type].


    That is why you can (and usually should) declare variables as the supertype
    (preferably an interface) and instantiate as the subtype (class):

    List <Foo> foos = new ArrayList <Foo> ();

    The object pointed to by 'foos' is both an 'ArrayList' and a 'List' (and an
    'AbstractList' and a 'Cloneable' and an 'Iterable' and a 'Collection' and a
    'RandomAccess' and a 'Serializable' and an 'AbstractCollection' and an
    'Object', while we're about it).
    <http://download.oracle.com/javase/6/docs/api/java/util/ArrayList.html>

    An instance of a 'javax.management.AttributeList' is all of those things as well.

    --
    Lew
    Ceci n'est pas une pipe.
    Lew, Dec 25, 2010
    #3
  4. Fred

    markspace Guest

    On 12/24/2010 4:15 PM, Fred wrote:

    > I don't understand why this is in general, possible. If an instance of
    > the subclass has been created, it cannot be assumed that an instance
    > of the superclass has been simultaneously created.



    Yes it can. That's what "inheritance" does, it creates the entire chain
    of super class for each sub class created.


    > And I don't understand why an assignment to a superclass's variable
    > would *ever* need to be made *by* the subclass. If the variable is not
    > static, the variable has been inherited - playing around with that
    > 'copy' of the variable should suffice for all purposes, should it not?



    I think it's to get around "hiding".

    class SubClass extends SuperClass {
    private int hawaii = 50;

    @Override
    public String toString() {
    super.hawaii = super.hawaii.toUpperCase();
    return super.hawaii + hawaii;
    }
    public static void main( String ... args ) {
    System.out.println( new SubClass() );
    }
    }

    class SuperClass {
    protected String hawaii = "Hawaii";
    }
    markspace, Dec 25, 2010
    #4
  5. Fred

    Roedy Green Guest

    On Fri, 24 Dec 2010 16:15:32 -0800 (PST), Fred
    <> wrote, quoted or indirectly quoted
    someone who said :

    >I don't understand why this is in general, possible. If an instance of
    >the subclass has been created, it cannot be assumed that an instance
    >of the superclass has been simultaneously created. If an instance of
    >the superclass has not been created, then the above statement doesn't
    >make sense. The assumption is that the hawaii variable is static,
    >right?


    An instance of this class has been created, which also contains all
    variables and methods of the superclass. There is only one big
    aggregate object. Fields of each subclass are tacked on the end,
    making a bigger and bigger snowball.

    There is no need to say super, unless you have overridden a method or
    shadowed a variable.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com
    A short order cook is a master of multitasking. Every movement is
    optimised from years of practice. Yet when a computer executes a
    multitasking program, it approaches the task as if for the first time.
    Roedy Green, Dec 28, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest

    super.super.super how?

    Guest, Feb 19, 2005, in forum: Java
    Replies:
    24
    Views:
    10,745
    Darryl Pierce
    Feb 24, 2005
  2. chriss
    Replies:
    3
    Views:
    459
    chriss
    Sep 10, 2005
  3. Evan Klitzke
    Replies:
    0
    Views:
    349
    Evan Klitzke
    Aug 2, 2007
  4. Ben Finney

    super() doesn't get superclass

    Ben Finney, Sep 18, 2007, in forum: Python
    Replies:
    53
    Views:
    1,302
    OKB (not okblacke)
    Sep 20, 2007
  5. bart van deenen
    Replies:
    6
    Views:
    739
    bart van deenen
    Mar 3, 2009
Loading...

Share This Page