An interface defines just that - an interface that is specified by a set of public methods, so use an interface rather than an abstract class whenever this is what you need. Use an abstract class as a base when you need field inheritance and or protected method inheritance as well as public methods. Java only permits single class inheritance so a class can only have one base class. On the other hand a class can implement any number of interfaces so interfaces provide a way to have multiple-inheritance of public methods. You can also use an interface to encapsulate a set of constants that you want to access in several different classes. Just make each class implement the interface.
Ivor Horton<br />Author of the Beginning Java Series including the new <a href="http://www.amazon.com/exec/obidos/ASIN/1861005695/ref=ase_electricporkchop" target="_blank" rel="nofollow">Beginning Java 2 SDK 1.4 Edition</a>
Hi new2java, If you want to keep hangin' around the Ranch you need to conform to the naming policy around here which is a first and last name for display. Now for your question. For old programmers like me Interfaces are the greatest thing since breaking the 1 Meg RAM barrier. What makes OO programing so powerful, and JAVA in particular is the use of interfaces. By using an interface you are essentially creating a contract that all who wish to implement the interface have to sign (metaphorically that is.) That leaves the implementor (class that implements the interface) to fulfill the contract anyway he wants. Clients who use the interface don't have to (nor do they need to) know anything but the interface. The implication of all this is, the implementing class can change (even the inteface can be extended) without breaking any existing clients. Abstract classes are more for large hierarchies of classes, where you put as much commanality as possible into the abstract class. Hope this helps Michael Morris SCJP
Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
Gee, thanks, Michael I couldn't have said it better. JavaRanch's naming policy. Your displayed name should be 2 separate names with more than 1 letter each. You need to have a compiant display name to win a book.
"JavaRanch, where the deer and the Certified play" - David O'Meara
Interface -- (1)Only contains set of abstract method. Doesn't contains instance methods. (2)Only contains Public methods. (3)Class has to implements all abstract methods of interface class. Abstract -- (1) Can contains abstract methods as well as instance methods (2) To use Abstract class, subclass needs to extends it. (3) Can contain private,public, protected, static methods. Thanks, Angela
Hi, Sorry.I made the changes according to the naming policy. I still have a question. When we implement the interface, all the methods have to be overwritten by all the classes that use this interface right? But are we not doing the same thing by extending an abstract class where in, if the class does not explicitly override the methods, still all of the methods are available? Please do correct me. I am still in confusion? Can somebody give an example please? Thanks, Tom Keith.
Not a lot of commentary, but perhaps it's enough. I recommend that you actually program, compile, and otherwise play with these classes to see how things work. Try to make them not work and figure out why what you did didn't work - make some fields private, or final, or static, try to give some methods implementations, etc. Good Luck. [ March 26, 2002: Message edited by: Dirk Schreckmann ]
If I nitpick at the author's response, am I then excluded from winning the book?
Originally posted by Ivor Horton: interfaces provide a way to have multiple-inheritance of public methods.
Just to point out one glaring fact... Public methods in an interface have no implementation. So, no implementation is inherited. As has been stated, the implementing class is agreeing to implement the methods in the interface - to provide the implementation. I've run into more than one person new to Java and OOP for whom this concept was a bit confusing at first, because the book they were reading said something like: "...implementing provides multiple inheritance in Java..." - at least that's how the newbie interpreted what the book said.