Skip to main content

Another Suggestion : 'Implements Final'

No replies
onderteker
Offline
Joined: 2004-11-25

The keyword 'implements' means 'is an implementation of'. Interfaces initially thought as a replacement for multiple inheritance. But, they play another role : separation of interface (specification) and implementation. However, in Java there is no way to say this class 'is the implementation of' that interface. (Not 'an', 'the' implementation of.) In some APIs such as JAXP, the implementation is found in runtime. Patterns such as Factory Pattern utilizes interfaces and abstract classes.

MyInterface mi=MyFactory.newMyInterface(...);

But, For constructor parameters you sould write a lot of factory method. Most of parameters simply passed to implementation class's constructor. Why can't we write

class MyInterfaceImpl implements final MyInterface{
public void myMethod(...){
...
}
}

and use like

MyInterface mi=new MyInterface();
mi.myMethod();

JVM can find the implementation. In this way both we could have a lot of alternative implementations as in JAXP without sacrificing simplicity.
* We should be able to define constructors in interfaces so that code may be checked in compile time
* For interaface com.xxx.MyInterface, there must be a file in the path com/xxx/MyInterface.implementation in which the name of implementation class written. Otherwise JVM can't find easily the name of implementation class.
* A similar construct 'extends final' should be used for abstract classes. No new keyword!