This week's book giveaway is in the Reactive Progamming forum. We're giving away four copies of Reactive Streams in Java: Concurrency with RxJava, Reactor, and Akka Streams and have Adam Davis on-line! See this thread for details.
1) a class can implement only one class (abstract or not), but implement multiple interfaces 2) an abstract class can provide partial or even full implementation, whereas interfaces have no implementation at all (every method is implicitly public abstract and every field is implicitly public static final)
I think of an abstract class as something an object can be. So you can extend and abstract class, as an example I would use a car, you have an abstract class Car, so a SportCar is an extension of a Car cause it can do the same things a Car can do, only that it could do them in a different fashion. An interface is a bridge, is some kind of specification you must comply in order to interact with a system. So you have a I/O interface to interact with your computer, and it can be a mouse or a keyboard, and interface is a kind of standard to help two systems interoperate, as a language, language is the interface which helps two persons to communicate.
She will remember your heart when men are fairy tales in books written by rabbits.<br /> As long as there is duct tape... there is also hope.
An abstract class is any class that is declared as "abstract"; this means that you cannot create an instance of that class, you must create an instance of a (concrete) subclass. Typically, although not all the time, an abstract class will have abstract methods--that is, methods that have a declaration but no implementation. These methods must be implemented in a subclass, or else that subclass must be declared as abstract as well and the methods must be implemented in some subclass of the subclass.
An interface is (at a basic level) an abstract class with no methods defined. In addition, it can only define member variables that are constant (final).
You would use an abstract class when you wanted to provide base functionality but require some specific details in some methods. An example would be a Vertebratum class. This class ensures that all animals in the class have a backbone, but you cannot actually instantiate a Vertebratum object. You would have to instantiate a HomoSapiens (Human) or a CanisDomesticus (Dog) subclass.
use an interface when you think that a large number of different things can perform similar functionality. An example would be a Bird and a Bat class. Bird is a subclass of Vertebratum, while Bat is a subclass of Mammalia (which is a subclass of Vertebratum). However, both Birds and Bats can fly, so they both implement the Flyer interface (along with a number of Insects)
(Note that in this example, Bird and Bat would both be abstract classes; also, the Penguin subclass of bird would throw some sort of exception (most likely java.lang.UnsupportedOperationException) when you asked it to fly, although it would implement the Swimmer interface)
Hope that this helps.
Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
You know it is dark times when the trees riot. I think this tiny ad is their leader:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop