I'd like to force the use of the super keyword when accessing a field in
a parent class. For example, if Parent contains
int foo;
and Child extends Parent, then any reference to foo in Child should be
written as:
super.foo = whatever;
The reason is that this convention makes the code much easier to follow.
I can't find a switch in Eclipse, though, that would give a warning if
the super keyword isn't there. Is there any other way to force it?
Robert Klemme - 07 Aug 2006 17:49 GMT
> I'd like to force the use of the super keyword when accessing a field in
> a parent class. For example, if Parent contains
[quoted text clipped - 9 lines]
> I can't find a switch in Eclipse, though, that would give a warning if
> the super keyword isn't there. Is there any other way to force it?
No. But direct field access should not occur anyway. Instead you
should make your fields private and access them via getter and setters only.
Regards
robert
Hendrik Maryns - 07 Aug 2006 18:49 GMT
Chris schreef:
> I'd like to force the use of the super keyword when accessing a field in
> a parent class. For example, if Parent contains
[quoted text clipped - 9 lines]
> I can't find a switch in Eclipse, though, that would give a warning if
> the super keyword isn't there. Is there any other way to force it?
There is no switch for super alone, but there is one for unqualified
field access, where this falls under. It will also warn you if you do
not use this.field. But as Robert says, getters and setters are the
better idea.
H.
- --
Hendrik Maryns
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
AndrewMcDonagh - 07 Aug 2006 19:05 GMT
> I'd like to force the use of the super keyword when accessing a field in
> a parent class. For example, if Parent contains
[quoted text clipped - 9 lines]
> I can't find a switch in Eclipse, though, that would give a warning if
> the super keyword isn't there. Is there any other way to force it?
Yes there is.....kind of anyway.
two choices:
1) As Robert says, make it private and provide a setter/getter
there's usually very few reasons members variables should be visible to
derived classes, like there is very little reason we'd expose them to
external classes.
2) Dont use Inheritance - use delegation
Instead of worrying about forcing derived classes to call super, worry
about the design you have chosen. Its a good rule of thumb (i.e. not
universal) to favor delegation over inheritance.
To use delegation, you will have to provide the same getters/setters as
you would in choice number 1).
Andrew
Dieter Lamberty - 08 Aug 2006 09:30 GMT
AndrewMcDonagh schrieb:
>> I'd like to force the use of the super keyword when accessing a field
>> in a parent class. For example, if Parent contains
[quoted text clipped - 30 lines]
>
> Andrew
Hi Andrew
Could you please explain your number 2.
In my oppinion there is a difference between inheritance and delegation.
This is part of the logic of the program.
For example:
class GeoObject {
private int size;
}
class Triangle extends GeoObject {}
and not
class Triangle {
private GeoObject delegate;
}
This is part of the (logical) design. Inheritance means a "is a"
relation, delegation means a "contains a" relation (or muliple
inheritance ;-) )
Or do you mean this only in this specific case. But how do you know this
without seeing the code...
But I agree with number 1.
Dieter
Ingo R. Homann - 09 Aug 2006 13:28 GMT
Hi,
> In my oppinion there is a difference between inheritance and delegation.
Exactly! The problem is that many people do not realize this difference
and tend to use inheritance when delegation would be apropriate. (Often,
because most languages support inheritance very easy (by a *single*
keyword for the whole class) while implementing delegation requires a
lot of work (every method must be delegated))
Ciao,
Ingo