1

I'm not exactly clear on what abstract classes or interfaces are in Java, so please bear with me. I'm coding a chess game and I have a class Piece and subclasses for each type of piece, ie. Knight, Rook, Pawn, etc. I would like methods in my Piece class to be inherited into each subclass, some without new code being written in the subclasses (ie a "getX()" method) and some which will need new code (a "checkValidMove" for each piece).

I currently am extending the Piece class for each subclass, but I would like some input on if this is the best method for writing my code before I continue and go too far before I realize I should've written it differently.

Aaron
  • 992
  • 3
  • 15
  • 33
  • This looks like a good design. – Yassin Hajaj Mar 28 '16 at 19:10
  • Inheriting from a class is done by extending it. They are basically the same thing in Java. And yeah, pretty solid design. It reflects the Object Oriented paradigm nicely. – Obicere Mar 28 '16 at 19:11
  • Not certain what you are asking here? Your title suggests you don't understand that you inherit from a parent class by using extends, but that just could be poor word choice. Generally you do want to make a parent class and have other classes extend from it when you have multiple similar objects that would share functionality and properties. – scrappedcola Mar 28 '16 at 19:12

2 Answers2

2

It sounds as if you're asking about when to subclass and when to implement to an interface.

In general, if you're in control of the class hierarchy and don't intend for others to make use of it, you'd create a subclass hierarchy. This allows you to make use of code in your parent class - which is what you want here; a Knight class should inherit code from a Piece class so Knights can do whatever Pieces can do. (In short: Knight extends Piece.)

If you want to allow others to make use of your classes (for instance, if you're providing a library) you would want to use interfaces. All methods in an interface are abstract, meaning the class that implements the interface has to implement those methods. For instance, you might have a Printer interface that knew how to outputGraphics and outputText - each individual printer would implement that differently (and might be implemented by different people or companies), but if they implemented the Printer interface they could be used by any program that knew about that interface. (In short: BrotherPrinter implements Printer, HPPrinter implements Printer, etc.)

user3486184
  • 2,147
  • 3
  • 26
  • 28
  • 1
    Related: http://stackoverflow.com/questions/10040069/abstract-class-vs-interface-in-java – RaminS Mar 28 '16 at 19:24
  • Thank you, I realize my title probably wasn't worded the best it could be, but I was mainly asking if I should use an interface or not since I'm not totally clear on what it is used for, but your explanation makes sense. – Aaron Mar 29 '16 at 00:25
-2

In Java there is no keyword called inherits. You "inherit" by writing extends.

But yes, writing subclasses for the different pieces is the way to go. Do not worry about having to write more lines of code - this is object-oriented programming. If you aim for high cohesion, low coupling, easy maintainability and readability, you are on the correct track.

RaminS
  • 2,208
  • 4
  • 22
  • 30