From the Blogosphere
What’s the Difference Between Abstract Classes and Interfaces?
Both abstract classes and interfaces allow you to implement common static methods
By: Yakov Fain
Aug. 18, 2014 12:56 PM
Probably the most popular question during Java technical interviews is “What’s the difference between abstract classes and interfaces”. In my own three year old book I’ve offered the following answer:
“An abstract class may contain code in method bodies, which is not allowed in an interface.With abstract classes you have to inherit your class from the abstract one because Java does not allow multiple inheritance. On the other hand, you can implement multiple interfaces in your class.”
But starting from Java 8 this answer is wrong. This is how I’d answered it now:
“Both abstract classes and interfaces allow you to implement common static methods while leaving the implementation of some methods to other classes. Abstract classes can also have instance-specific methods and they enforce vertical inheritance hierarchy. Interfaces can’t have instance methods, but they don’t enforce any class hierarchy, so your class can extend any other class while implementing multiple interfaces.“
So abstract classes and interfaces have subtle differences now, which are good to recognize anyway. You can find more details on this subject in the draft of Chapter 5 of my upcoming Java for Kids book.
SOA World Latest Stories
Subscribe to the World's Most Powerful Newsletters
Subscribe to Our Rss Feeds & Get Your SYS-CON News Live!
SYS-CON Featured Whitepapers
Most Read This Week