Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Abstract class does not need to implement interface methods?  RSS feed

 
Ryan McClain
Ranch Hand
Posts: 153
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quote from a book:
When an interface inherits other interfaces, any class that implements that interface must implement all of the methods declared by that interface and the inherited interfaces. For example, if a class implements the ProductDAO class, it must implement all of the methods defined by the ProductReader and ProductWriter classes. If it doesn't, the class must be declared as abstract so that no objects can be created from it.


I don't understand that last sentence. Why does it make it ok for a class to implement an interface but not its methods when it's declared abstract? What does the inability of creating objects from this class have to do with implementing methods from its implemented interfaces?
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It means the concrete classes extending your abstract class will have to implement the interface methods if they aren't already implemented in the abstract class. You can implement some of the interface methods in your abstract classes, the rest will have to be implemented in your concrete classes extending the abstract class.
 
Campbell Ritchie
Marshal
Posts: 55751
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can create instances of abstract classes, but by indirect routes.
You know that abstract classes are allowed abstract methods? Well, the unimplemented methods in interfaces are abstract methods. If you create an object you must implement them, so whenever you create a concrete subclass of an abstract class (or an anonymous class), you must implement all its methods. You can create instances of the concrete subclasses directly, so they must have all methods implemented.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!