The Artima Developer Community
Sponsored Link •

Design Forum
rfe.request Java feature. enforce call to super.bar() with 'super' keyword

2 replies on 1 page. Most recent reply: Jan 30, 2003 3:49 PM by Anil Philip

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Anil Philip

Posts: 19
Nickname: anilphilip
Registered: Nov, 2002

rfe.request Java feature. enforce call to super.bar() with 'super' keyword Posted: Jan 20, 2003 10:39 AM
Reply to this message Reply
Advertisement
Hello,
I was wondering if the feature described below would
be useful in Java. I submitted an RFE but wanted someone else also to see it whether it makes sense.
thanks,
Anil Philip
----------

Situation:
the 'abstract' keyword helps force control to flow
from a parent class to the inheriting child class.

<--- --<- base class
| |
| |
| V
<---- child class

For example,
// currently
class Foo {
void someMethod() {
// do something here
}
abstract void bar(int i);
}
Since bar() is abstract, this forces control to
transfer to the implementing method of the inheriting
concrete class when invoked.

How can we insist upon the reverse? How can an
overriding method be forced to call super.bar()?

<---- base
| ^
| |
<--- --<-- child

Currently there is no way to enforce this (making it
final will not allow bar() to be overridden).
This feature could be useful in layered software where
the base class bar() performs basic processing
and we wish to insist that all overriding bars() in
inheriting classes perform this basic processing in
addition to whatever behavior they add.


Feature requested:

if a method has the 'super' keyword (or perhaps
'abstract' might be more appropriate?) prepended,
then it is allowed to have a body if it so wishes.
Any overriding method then *must* call its superclass
method at some point in its body. This will
be enforced at compile time.

For example:

//feature requested
class Foo {
void someMethod() {
// do something here
// ...
}
// N.B. it has a body now. Note the new keyword
super /* or 'abstract' ? */ void bar(int i) {
// do some processing here based upon param 'i'
// ...
}
}

class FooChild extends Foo {
/**
N.B. super.bar() is called at some point
*/
void bar(int i) {
// do some processing here
// ...
super.bar(i);
// continue and finish off processing
// ...
}
}


Real world example:

In AWT when one wishes to do low level event
processing,
one overrides the processXEvent(), implements any
special
processing and then invokes super.processXEvent() so
that other registered listeners will also be notified. If
processXEvent() had the 'super' keyword, then at compile time we could have ensured that other listeners will be notified. Thus this can be enforced by the base class method.
super void processKeyEvent(KeyEvent);

for example,
when you wish to trap the KeyEvent
to close window when you type 'q' or 'Q'

// standard example from a Java textbook
public void processKeyEvent(KeyEvent evt) {
if(evt.getID() == KeyEvent.KEY_TYPED &&
(evt.getKeyChar() == 'q' or evt.getChar() == 'Q')) {
dispose();
System.exit(1);
}
super.processKeyEvent(evt); //calls other listeners
}

=====
for good news go to http://members.tripod.com/goodnewsforyou/goodnews.html


Joan Friedman

Posts: 6
Nickname: joan
Registered: Jan, 2003

Re: rfe.request Java feature. enforce call to super.bar() with 'super' keyw Posted: Jan 30, 2003 3:06 PM
Reply to this message Reply
Hello. It's a thoughtful suggestion, but it has two drawbacks. The first is that it doesn't gain anything - in order to place the call to 'super' properly, the programmer has to understand what the over-ridden method is doing, which is the same situation we have without the additional keyword. The second problem is that sometimes you need to short-circuit the usual behavior and *not* call the superclass method. For example, usually event listeners want to call the superclass and continue the chain - but sometimes, it's important to consume the event and interrupt the chain.

Anil Philip

Posts: 19
Nickname: anilphilip
Registered: Nov, 2002

Re: rfe.request Java feature. enforce call to super.bar() with 'super' keyw Posted: Jan 30, 2003 3:49 PM
Reply to this message Reply
Joan,
Thanks for your reply.
The use of the 'super' keyword in the declaration would be optional. But when it is used, it would ensure that the superclass method is called at some time. Just as we have keywords in the language to enforce some design 'wishes'.

eg. in C++, the use of 'const' (or 'final' in Java) for the parameters/return values makes them non-modifiable by the 'user' of the variables.

regards,
Anil Philip

> Hello. It's a thoughtful suggestion, but it has two
> drawbacks. The first is that it doesn't gain anything - in
> order to place the call to 'super' properly, the
> programmer has to understand what the over-ridden method
> is doing, which is the same situation we have without the
> additional keyword. The second problem is that sometimes
> you need to short-circuit the usual behavior and *not*
> call the superclass method. For example, usually event
> listeners want to call the superclass and continue the
> chain - but sometimes, it's important to consume the event
> and interrupt the chain.

Flat View: This topic has 2 replies on 1 page
Topic: designing with extension Previous Topic   Next Topic Topic: Designing a function

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use