Referencing an interface before compiling - ( beginner's question )

J

Jean-Benoit MORLA

Hi
Given this interface:

public interface Buffer{
public void set( int value );
public int get();
}
( compiles ok )
How come this compiles?:

public class Producer extends Thread {
private Buffer sharedLocation; // reference to shared object
..
..

I thought that if I wanted to use an interface I had to "implement" it
and declare all its methods.

This thread-testing program hierarchy continues with:
public class UnsynchronizedBuffer implements Buffer {
..
..
Many thanks for your reply
 
V

VisionSet

Jean-Benoit MORLA said:
Hi
Given this interface:

public interface Buffer{
public void set( int value );
public int get();
}
( compiles ok )
How come this compiles?:

The compiler mainly does a check to make sure the syntax is correct, it
produces a class file, but this is just a flag so that you can do what you
have done below without the complier protesting.
public class Producer extends Thread {
private Buffer sharedLocation; // reference to shared object
.
.

I thought that if I wanted to use an interface I had to "implement" it
and declare all its methods.

You do, you can't do anything with your 'sharedLocation' as it hasn't been
instyantiaated, and to do that you need a concrete implementation.

The interface is a specification it presents a contract that must be
honoured you can reference the contract, but to use it you must provide an
implementation.
 
G

Gerrit Hulleman

Jean-Benoit MORLA said:
Hi
Given this interface:

public interface Buffer{
public void set( int value );
public int get();
}
( compiles ok )
How come this compiles?:

public class Producer extends Thread {
private Buffer sharedLocation; // reference to shared object
.
.

I thought that if I wanted to use an interface I had to "implement" it
and declare all its methods.

This thread-testing program hierarchy continues with:
public class UnsynchronizedBuffer implements Buffer {
.
.
Many thanks for your reply

Hope I am not to simplistic about this, can't judge your knowledge form
here. Sorry if I offend....


Maybe better explained using an example: Car.

Say you define a Car as: four wheels, one steering wheel. (your interface
buffer) It is a valid definition, and so it exists. (and your interface
compiles)

Next you give a cardealer a place for a Car. The dealer does not have to
know what kind of car, as long as it is a car. (your private Buffer
sharedLocation;). Your declared variable shareLocation is only a reference
to a buffer, but the buffer does not exist yet: An empty parking space at
the cardealer

The problem is the following: there is no such thing as JUST a car. There
are things that ARE a car, because they are conform the definition. An
implementation of a car is eg. The Renaul picasso model 3-a, serial
34346-098235324. This thing exists (only one! like any object it is unique).
You can give this renault as a car to the car dealer and it will not know
what kind of car without further research (casting and such). The dealer
stores the given car in his parking space and is able to use the wheel.

Programming is not like real life, where you say something is a car just
because it has four wheels and a steering wheel. You must specify that an
object is a car by using the implementing tag (implements Buffer). By
implemeting you MUST have four wheels and a steering wheel. Everything else
is nice, but not required to pass as a car.

Hope this helps...
Gerrit
 
J

John C. Bollinger

Jean-Benoit MORLA said:
Hi
Given this interface:

public interface Buffer{
public void set( int value );
public int get();
}
( compiles ok )
How come this compiles?:

public class Producer extends Thread {
private Buffer sharedLocation; // reference to shared object
.
.

I thought that if I wanted to use an interface I had to "implement" it
and declare all its methods.

Your confusion appears to arise from what it means to you to "use" an
interface. You would perhaps be better off to drop the term altogether
in favor of more specific language:

(1) A class may implement an interface. That means the class asserts
that it provides implementations for zero or more of the methods
declared by the interface, but more importantly, declares that the class
is a subtype of the interface. The class need not provide
implementations for all the interface's methods; if it does not do so,
however, then it must declare itself abstract. The class cannot declare
methods that conflict with those defined by the interface. The compiler
checks all these constraints.

(2) A class may declare variables, method arguments, and / or method
return values to have the interface for their type. Indeed, this is key
to making interfaces useful. A class that does these things is not
thereby required to implement the interface -- rather, the objects
stored, passed, or returned must be of classes that implement the
interface. (Technically it is the objects' references that are stored,
passed, or returned, not the objects themselves; consider the above to
be a simplification employed for purposes of prose clarity.)

(3) A class may transiently handle expressions whose declared type is an
interface, without explicitly naming the interface anywhere. E.g.:

[...]

o.doSomethingImportant(p.returnsAnInterfaceType());

[...]

where the declared return type of returnsAnInterfaceType() is, as the
name indicates, an interface, and the required type of the argument to
doSomethingImportant() is either Object or a compatible interface type.
There are no special constraints on that situation beyond the normal
Java type compatibility rules.

I hope that helps.

John Bollinger
(e-mail address removed)
 
J

Jean-Benoit MORLA

John C. Bollinger said:
Jean-Benoit MORLA said:
Hi
Given this interface:

public interface Buffer{
public void set( int value );
public int get();
}
( compiles ok )
How come this compiles?:

public class Producer extends Thread {
private Buffer sharedLocation; // reference to shared object
.
.

I thought that if I wanted to use an interface I had to "implement" it
and declare all its methods.

Your confusion appears to arise from what it means to you to "use" an
interface. You would perhaps be better off to drop the term altogether
in favor of more specific language:

(1) A class may implement an interface. That means the class asserts
that it provides implementations for zero or more of the methods
declared by the interface, but more importantly, declares that the class
is a subtype of the interface. The class need not provide
implementations for all the interface's methods; if it does not do so,
however, then it must declare itself abstract. The class cannot declare
methods that conflict with those defined by the interface. The compiler
checks all these constraints.

(2) A class may declare variables, method arguments, and / or method
return values to have the interface for their type. Indeed, this is key
to making interfaces useful. A class that does these things is not
thereby required to implement the interface -- rather, the objects
stored, passed, or returned must be of classes that implement the
interface. (Technically it is the objects' references that are stored,
passed, or returned, not the objects themselves; consider the above to
be a simplification employed for purposes of prose clarity.)

(3) A class may transiently handle expressions whose declared type is an
interface, without explicitly naming the interface anywhere. E.g.:

[...]

o.doSomethingImportant(p.returnsAnInterfaceType());

[...]

where the declared return type of returnsAnInterfaceType() is, as the
name indicates, an interface, and the required type of the argument to
doSomethingImportant() is either Object or a compatible interface type.
There are no special constraints on that situation beyond the normal
Java type compatibility rules.

I hope that helps.

John Bollinger
(e-mail address removed)

Thank you for these replies. It's going to take a while to sink in :)
Jean-Benoit MORLA
(e-mail address removed)
 

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,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top