Re: Java "interface" vs. OO interface

From: Dave Harris (brangdon_at_cix.co.uk)
Date: 11/22/03


Date: Sat, 22 Nov 2003 13:29 +0000 (GMT Standard Time)

tgm2tothe10thpower@hotmail.replaceTextWithNumber.com (Thomas G. Marshall)
wrote (abridged):
> > I think Java uses interfaces instead of classes because the designers
> > couldn't figure out a good implementation of MI of classes. The
> > ideology was a post-hoc justification.
>
> No. Interfaces allow entire subsystems to be describable in java
> without a single class being supplied, abstract or otherwise.

This is achieved by changing the terminology so that some abstract classes
are not called classes any more. It's just terminology; it's not letting
you do anything new.

> There is elegance and just plain old good sense in having a
> language construct actually personify the contract without
> implementation.

Except Java doesn't do that, because it doesn't support pre-conditions or
post-conditions. One of the uses of non-abstract interface classes in C++
is to provide wrapper functions which do such checks. Along the lines of:

    class MyInterface {
    public:
        int get_data( int index ) {
            assert( 0 <= index && index <= 100 );
            int result = get_data_impl( index );
            assert( 0 <= result && result <= 10000 );
            return result;
        }
    protected:
        virtual int get_data_impl( int index ) = 0;
    };

Notice that get_data() is not virtual, so that the checks cannot be
circumvented. This also shows it can make sense for interfaces to have
non-public methods.

Sometimes interfaces want to have code. Another example is where a default
implementation can be written in terms of the pure virtual methods. In
this case the implementation /is/ virtual, so it can overridden where
necessary, so it doesn't represent a commitment to representation.

One use of such virtual methods with default implementations is evolving
an interface forward without breaking old subclasses. You can add new
methods, and old subclasses don't need to change to implement them. And
this use shows that giving the pure abstract base class a special keyword
is a mistake. It would /force/ the subclasses to change, from "implements"
to "inherits".

-- Dave Harris, Nottingham, UK



Relevant Pages

  • Re: dynamic type checking - a pauline conversion?
    ... to actually write the code in Java. ... Is the query handled ... One of the problems with Java typing is small differences in interfaces ... array iteration, List iteration, and database query result iteration all ...
    (comp.object)
  • Re: CLOS Properties Question
    ... In interfaces, you can declare methods, but you ... This mess was the result of the fact that interfaces ... > is even an OOPSLA or ECOOP paper published before the advent of Java ... > Back to the question what this all has to do with multiple inheritance: ...
    (comp.lang.lisp)
  • Re: Python vs C for a mail server
    ... that is mostly unnecessary with Python. ... > I don't have enough experience with languages that provide interfaces, ... > such as Java, to understand how useful they are, but it's my impression ... I can't comment on "most Java APIs", but if you look at established ...
    (comp.lang.python)
  • Re: MRO theory
    ... It creates a Python class to proxy any Java class or Java ... Java proxies go, when it creates a class it can have at most one ... The problem basically comes about because interfaces are not classes, ...
    (comp.lang.python)
  • Re: Java "interface" vs. OO interface
    ... > I think Java uses interfaces instead of classes because the designers ... To think that the designers of Java and .NET couldn't figure out a good ... Enough for a language to adopt a new keyword ...
    (comp.object)