aspose file tools*
The moose likes Beginning Java and the fly likes using interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "using interfaces" Watch "using interfaces" New topic
Author

using interfaces

marco marco
Greenhorn

Joined: Dec 11, 2004
Posts: 5
Hi,
trying to workout the benefits of using interfaces. Came across a book that talks about loose coupling between two classes say A and B and to create an interface bteween the two so that Class A and talk to Class B thru the interface.

Here is a snippet :

// Class A wants to talk to class B thru an interface

class A {
int i =5;
void aMethod() {
System.out.println(�A talking�);
}

}

interface AB {

}

class B implements AB{
int b= 3;
public B() {

}
void bMethod() {
System.out.println(�B talking�);
}


}
Rusty Shackleford
Ranch Hand

Joined: Jan 03, 2006
Posts: 490
Interfaces are not used so 2 objects can 'talk'. Your interface does nothing at all.

An interface is a way to enforce certain behaviors in a class without defining how that class implements the behavior. It is used as a workaround to multiple inheritance, and to keep objects loosely coupled, which means they can interact without knowing how the other object does what it does. That way a class does not need to create a concrete object. It plays a major part in software design, most notably design patterns.

A good, and simple example is the Comparable interface.

public Interface Comparable
{

public int compareTo(T o)

}

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Comparable.html
[ February 24, 2006: Message edited by: Rusty Shackleford ]

"Computer science is no more about computers than astronomy is about telescopes" - Edsger Dijkstra
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by marco marco:
... Came across a book that talks about loose coupling between two classes say A and B and to create an interface bteween the two so that Class A and talk to Class B thru the interface...

Hmmm... I think the word "interface" is being used here in a less technical sense. In this context, the interface is just something that acts as a "go between" for different objects -- kind of a central "communications hub." This way, the objects themselves only need a reference to the "hub," and don't need to know anything at all about the other objects involved (loose coupling).

Now, to actually implement this, a "real" Java interface or two would be helpful. Does your book provide any examples?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marco marco
Greenhorn

Joined: Dec 11, 2004
Posts: 5
Thanks for the reply. Here is an extract:
The less two classes know about each other the more lossely coupled they are. to each other. A very common approach when class A wants to use methods in Class B is to create an interface between the two. Once class B implements this interface , class A can use class B via the interface. This is useful later on you can use an updated class B or even a entirely different class, as long as it holds the contract of the interface.

There are no simple examples after the extract to demonstrate this.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Okay. This is part of the concept of "programming to an interface." Consider the following...

Note that A has a Thing that it can use. Of course, Thing is just an interface, so what A really has is a reference to an instance that implements Thing. And that reference has been upcast to the interface type (for example, Thing t = new B(); ).

Now, B and C might implement the methods in Thing very differently. But the important detail is that A doesn't know -- or care -- whether it's using an instance of B or C. All A cares about is that this instance behaves as defined by the interface Thing.
marco marco
Greenhorn

Joined: Dec 11, 2004
Posts: 5
great i understand now. Thanks for your advice.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: using interfaces
 
Similar Threads
String addition
Threads,serialisation, static variables - java basic doubts
Which class are Methods of HttpServletRequest implemented in?
Same constant names in different Interfaces
a problem of multiple inheritance