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

Discussion in 'Java' started by Jean-Benoit MORLA, Jul 15, 2004.

  1. 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
    Jean-Benoit MORLA, Jul 15, 2004
    #1
    1. Advertising

  2. Jean-Benoit MORLA

    VisionSet Guest

    "Jean-Benoit MORLA" <> wrote in message
    news:...
    > 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.

    --
    Mike W
    VisionSet, Jul 15, 2004
    #2
    1. Advertising

  3. "Jean-Benoit MORLA" <> wrote in message
    news:...
    > 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
    Gerrit Hulleman, Jul 15, 2004
    #3
  4. Re: Referencing an interface before compiling - ( beginner's question)

    Jean-Benoit MORLA wrote:

    > 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
    John C. Bollinger, Jul 15, 2004
    #4
  5. "John C. Bollinger" <> wrote in message news:<cd6als$52q$>...
    > Jean-Benoit MORLA wrote:
    >
    > > 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
    >


    Thank you for these replies. It's going to take a while to sink in :)
    Jean-Benoit MORLA
    Jean-Benoit MORLA, Jul 16, 2004
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=

    No Class at ALL!!! beginner/beginner question

    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=, Feb 2, 2005, in forum: ASP .Net
    Replies:
    7
    Views:
    556
    =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?=
    Feb 3, 2005
  2. Rensjuh
    Replies:
    7
    Views:
    945
    Mabden
    Sep 2, 2004
  3. Garrett Cooper
    Replies:
    0
    Views:
    541
    Garrett Cooper
    Feb 24, 2009
  4. Garrett Cooper
    Replies:
    0
    Views:
    485
    Garrett Cooper
    Feb 24, 2009
  5. william nelson

    Beginner's Beginner

    william nelson, Apr 11, 2011, in forum: Ruby
    Replies:
    7
    Views:
    196
    7stud --
    Apr 12, 2011
Loading...

Share This Page