Jump to content

Interface help..~

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
Guest_R3.RyozKidz_*

Guest_R3.RyozKidz_*
  • Guests
This is my interface :
public interface Buffer
{
    public void set(int value) throws InterruptedException;
    
    public int get() throws InterruptedException;
    
}

This is the class that implements the interface
import java.util.concurrent.ArrayBlockingQueue;

public class BlockingBuffer implements Buffer
{
    private final ArrayBlockingQueue<Integer> buffer;
    
    public BlockingBuffer()
    {
        buffer = new ArrayBlockingQueue<Integer>(1);
    }
    
    public void set(int value) throws InterruptedException
    {
        buffer.put(value);
        System.out.printf("%s%2d\t%s%d\n" , "Producer writes " , value , "Buffer cells occupied: " , buffer.size());
    }
    
    public int get() throws InterruptedException
    {
        int readValue = 0;
        
        readValue = buffer.take();
        System.out.printf("%s %2d\t%s%d\n" , "Consumer reads " , readValue , "Buffer cells occupied: " , buffer.size());
        
        return readValue;
    }
}
This is my main method
public static void main(String args[])
    {
        ExecutorService application = Executors.newCachedThreadPool();
        
        Buffer sharedLocation = new BlockingBuffer();
        
        application.execute( new Producer( sharedLocation));
        application.execute( new Consumer( sharedLocation));
        application.shutdown();
    }

what i want to ask is about this statement:
Buffer sharedLocation = new BlockingBuffer();
I can't understand this statement . Can an interface variable holds the reference of an object ..? Is interface acted like inheritance ..?

thanks in advance .:rolleyes:

#2
ZekeDragon

ZekeDragon

    Writes binary right handed and hex left handed

  • Moderators
  • 2,103 posts

R3.RyozKidz said:

I can't understand this statement . Can an interface variable holds the reference of an object ..? Is interface acted like inheritance ..?
Yes and yes. In fact, this is the point of an interface. Frequently when you want to design an object that is supposed to interact with a certain kind of object (IE an object that can do certain things, for example take a count), you'd write an interface for it and then program to that interface. Take this code for example:

public interface SimpleCounter
{
    void iterateCount();
    int returnCount();
}
public class LetterCounter
{
    public static void countLetters(SimpleCounter counter, String str)
    {
        for (int i = 0; i < str.length(); ++i)
        {
            counter.iterateCounter();
        }
        System.out.println("" + counter.returnCount());
    }
}
The only methods you are using are the interfaces methods. You can also use an interface that's returned from another method:
SimpleCounter count = MyClass.makeSomeCounter();
Yes, that means you can also create a new object that implements that interface and attach it to an interface reference.
SimpleCounter count = new ComplexCounter();
And then use it as a SimpleCounter object. :)
Wow I changed my sig!

#3
Guest_R3.RyozKidz_*

Guest_R3.RyozKidz_*
  • Guests
Can i know what cause the interface behaves like this ? (for class casting , i.e, downcasting , due to the late binding or dynamic binding )
can you explain more detail about
SimpleCounter count = MyClass.makeSomeCounter();
Or perhaps you can give me a code and i will figure the rest with the code of yours

And btw , for the interface SimpleCounter and class LetterCounter , should it have at least a class which implements interface SimpleCounter before any methods in the interface can be used in the LetterCounter class..?

Edited by R3.RyozKidz, 28 March 2010 - 12:51 AM.