type
is an unqualified name, and refers to a local variable, parameter, or field.
this.type
refers to a field accessible to the current class.
super.type
refers to a field accessible to the base class.
Since the subclass Cougar
does not have a field named type
, both this.type
and super.type
refers to the type
field declared in base class Feline
. In your example, there is no difference between this
and super
.
The statement type = "c ";
in method go()
is unqualified, and since there is no local variable or parameter by that name, it also refers to field type
of base class Feline
. As such, type
, this.type
, and super.type
all refer to the one and only field named type
.
If the statement in method go()
is changed to String Type = "c";
, then it defines a differently named local variable. Remember, Java names are case-sensitive, so Type
and type
are not the same name. So, field type
retains the initialized value of "f "
.
If you intended to change the statement in method go()
to String type = "c";
, then it defines and initialize a local variable named type
. By nature, it cannot update field type
, since the initializer applies to the newly declared local variable. So, field type
retains the initialized value of "f "
.
If you first declare the local variable in method go()
using String type;
, then assign it like you original code does using type = "c";
, then the unqualified name refers to the local variable, not the field, by that name. The local variable is hiding the field of the same name. So, once again, field type
retains the initialized value of "f "
.