i know what an interface is. but i dont understand what the use of connecting the objective reference to the interface method and then use it to implement..

could someone explain to me the code..whats going on/

interface TextReceiver {
        void receiveText( String text );
    }

    class TickerTape implements TextReceiver {
        public void receiveText( String text ) {
            System.out.println("TICKER:\n" + text + "\n");
        }
    }

    class TextSource {
        TextReceiver receiver;

        TextSource( TextReceiver r ) {
            receiver = r;
        }

        public void sendText( String s ) {
            receiver.receiveText( s );
        }
    }

Recommended Answers

All 8 Replies

Having a class implement an interface adds a 'type' to the class. Java uses typing to control the use of arguments as objects. Args must be of the correct type to be passed to a method.
An interface also guarantees that the object being passed as an arg has the expected methods that the callee wants to use to communicate with the passed object.

In your example, an object of type: TextReceiver has the method: receiveText(String)
that can be used by the TextSource class.

so you make the receiver reference an object reference (by making it equal to r),
then the method sendText..sents string s to the interface and then i implement the interface on TickerTape class.

is that how it works,
1) create a reference to the interface (reciever).
2) use a constructor to allocate a reference (r) and make it equal to the object reference,
3)then call the interface by a method (sendText), which would put an argument/parameter in it
4)implement teh interface on a class(TickerTape).

is that the way it needs to work to feed and implement the interface?
why is it useful?

1) Create a class(TickerTape) that implements the interface(TextReciever)
2) Create an instance of that class with a reference to it: TickerTape tt = new TickerTape();
3) Pass that reference(tt) (its of the correct type) to the constructor(new TextSource(tt))
4) Save that reference for later use (receiver = r)
5) Use the reference to call the method(r.sendText())

Norm1 if you look , tt was never created in my code

Sorry, I missed what you are asking. I thought you wanted to know about interfaces, how to define and use them.
What is your question?

i know what an interface is. but i dont understand what the use of connecting the objective reference to the interface method and then use it to implement..

Take a look at the Strategy Pattern. A bit more practical explanation.

is that how it works,
1) create a reference to the interface (reciever).
2) use a constructor to allocate a reference (r) and make it equal to the object reference,
3)then call the interface by a method (sendText), which would put an argument/parameter in it
4)implement teh interface on a class(TickerTape).

Not really. Read norm's first post again.
This is a better version of your post:
1. create a reference variable that holds references to objects that implement the interface
2. Use a constructor to create an instance of a class that implements the interface
3. Call the sendText method on that object. The compiler knows that's ok because the class implements the interface and the interface defines that method.

Think of it as if the interface only exists at compile time. It tells the compiler what methods your object has, but at runtime the calls are always to actual methods in an actual object, not to the interface.

It's useful becuase you may have many different classes that share something in common and the interface is a way of defining that. Eg. The Java API defines an interface List which defines methods for adding, removing, retrieving (etc) items from a list of items. There are many classes in ther API that can hold a list of items (ArrayList, Vector, LinkedList, Stack etc), but they all implement, in their own different ways, the methods of the List interface. That means you can access the contents of any of those classes by using those methods, without caring about exactly what kind of list they are.

commented: THANKS . i saved your comment on a word file +0

Thanks James, i got it now..
i think i need to remember than syntax/trick to feed and implement the interface on a class

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.