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

F

Fred

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
 
A

Arne Vajhøj

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
 
L

Lew

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.

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

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

Unless it's private, or package-private from a different package.
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.
 
M

markspace

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";
}
 
R

Roedy Green

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,733
Latest member
LonaMonzon

Latest Threads

Top