Force the use of super.

C

Chris

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?
 
R

Robert Klemme

Chris said:
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?

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
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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

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?

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
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFE1305e+7xMGD3itQRAkiGAJ0Xe0AqUjs3rG2UWnTFpVQc2odLaQCeLLEp
6Wa4CCDKb2b1jt6EDru0o3k=
=qCFQ
-----END PGP SIGNATURE-----
 
A

AndrewMcDonagh

Chris said:
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?

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
 
D

Dieter Lamberty

AndrewMcDonagh said:
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

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
 
I

Ingo R. Homann

Hi,

Dieter said:
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
 

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,776
Messages
2,569,602
Members
45,182
Latest member
BettinaPol

Latest Threads

Top