Interface
Hi,
I'm not able to understand the actual use of interfaces.
For eg I have Class A extends b implements c,d
I have 2 interfaces(c,d) which has got 4 methods and one abstract class 'b'.
What if I remove the 2 interfaces c,d and write all the methods in the Abstract class 'b' so that I dont have the burden of the two interfaces c,d.
Can someone explain me this.
Thanks.
Well, then you can't treat your class as a C or D. Which in turn causes you to lose some flexibility.
you can do that, and if having interfaces C and D is a burden to you then it sounds like you should. But if you ever need to define a class that only supports the behavior currently described by interface C then you will have to extend abstract class B and then provide empty implementations of all methods found there, which includes what is currently interface B.
Your design should tell you if you need to define interfaces for given sets of methods. If you remove all interfaces just to get rid of the "burden" of writing them, then your design is no longer capable of saying "this class is-a C" - everything now is-a B, and if that makes sense in your design then do it, but if not then don't get rid of the interfaces just to save some typing.
Good Luck
Lee
tsith at 2007-7-5 21:08:17 >

No I'm trying to understand the importance of using interfaces .
By putting whatever methods I have in interfaces in an abstract class I can acheive whatever I want.
But I want to understand the concept of 'interface' being used in java.
is it soemthng like a remote kind of stuff?
interfaces can be used to support distributed systems (remote kind of stuff) - but most simply put they are just desciptions of behavior that your class promises to implement, thus supporting an "is-a" relationship with the interface. By saying "I implement interface B" you are, in effect, saying "I am a B". The difference between an interface and an abstract class is that the abstract class may define some implementation, whereas the interface does not (it only describes behavior).
Search the fora for discussions about the difference between an abstract class an an interface, you'll find tons of information.
Lee
tsith at 2007-7-5 21:08:17 >

[url=http://mindprod.com/jgloss/interfacevsabstract.html]Java Glossary : interface vs abstract class[/url]
[url=http://www.javaworld.com/javaworld/javaqa/2001-04/03-qa-0420-abstract.html]Abstract classes vs. interfaces: When does it make sense to choose an abstract class over an interface?[/url]
[url=http://www.javaworld.com/javaworld/javaqa/2001-08/03-qa-0831-interface.html]Abstract classes and interfaces practicum: Move from theory to practice on when to employ abstract classes vs. interfaces[/url]
[url=http://java.sun.com/developer/JDCTechTips/2001/tt1106.html#tip2]Tech Tips: ABSTRACT CLASSES VS. INTERFACES[/url]
> Hi,
> I'm not able to understand the actual use of
> interfaces.
Interfaces are an abstraction that only defines the API and not the implementation. They are highly useful for many situations. The advantage they have over abstract classes is manyfold. First and foremost is that you can use Multiple Inheritance with an interface but not with an abstract class.
For example:
public abstract class Table {
// ... etc
public int getNumberOfLeggs();
// ... etc
}
public abstract class RollingFurniture {
// ... etc
public int getNumberOfWheels()
// ... etc
}
public abstract class RollingTable extends Table, RollingFurniture { // wait ... this wont compile
// ... etc
public boolean hasWheel(final int legNumber);
}
Now we cant aggregate features of Table and RollingFurniture because we cant use Multiple inheritance with classes. With interfaces, it becomes easier:
public interface Table {
// ... etc
public int getNumberOfLeggs();
// ... etc
}
public interface RollingFurniture {
// ... etc
public int getNumberOfWheels()
// ... etc
}
public interface class RollingTable extends Table, RollingFurniture { // no problem
// ... etc
public boolean hasWheel(final int legNumber);
}
Here we can aggregate the features.
In addition to these benefits, there are a lot of other architectural benefits to keeping an API distinct from an implementation. For example, consider if you were developing a banking application that communicated with the remote server. You want to provide the client with as little access as possible to increase security. Therefore you only give them the API and not the implementation. One other example is the situation when you have several implementations of the same API in potentially radically different manners (for example, one might be using a pure JNI solution). In this situation its easier to make both implementations implement the api.
-- Kraythe
[url http://www.javaworld.com/javaworld/jw-09-2001/jw-0921-interface.html]Design with interfaces and abstract classes to satisfy both type and implementation issues [/url]