0

I am making a chess game and trying to figure out if there is a way to make my abstract class "Piece" have a method that returns a new instance of the concrete implementations like Pawn or Rook.

For example:

static public Piece newNorthPiece(){ return new Piece(true); } 

Except instead of returning a Piece I want it to return whatever the class that called the method is. So if I call Pawn.newNorthPiece() I want it to return to me a new Pawn(true). And I would like to do this without having to write a new factory method for every class that extends the Piece class.

8
  • You need to call a concrete implementation. So either KnightPiece, PawnPiece etc. Your factory methods should determine which implementation to call. Do you have Pawn defined yet? Commented Apr 9, 2017 at 22:31
  • I am fairly certain this is not possible. The superclass isn't aware of its subclasses. The real question is why you don't want to write a new factory method for every class. Commented Apr 9, 2017 at 22:32
  • Obicere I'm not sure what you mean by that, but I have made classes for all the pieces already yes. Commented Apr 9, 2017 at 22:35
  • @OdinThorsen He is telling you to write a separate factory method for each class. Commented Apr 9, 2017 at 22:36
  • Gendarme because I try not to repeat myself, and I hoped there would be a way to just make this once in the abstract class, it would be much neater that way Commented Apr 9, 2017 at 22:36

3 Answers 3

1

Can I make a static factory method in an abstract class?

Yes, you can create a static method inside abstract class and the method will look the below:

public abstract class PieceFactory { public static Piece getPiece(String pieceType){ switch(pieceType) { case NorthPiece: return new NorthPiece(); case Pawn: return new Pawn(); } } } 

And you can call the PieceFactory.getPiece("Pawn") whichn returns the instance of Pawn.

Sign up to request clarification or add additional context in comments.

3 Comments

Not exactly what I was looking for because I would like the Piece class to not know what classes would be extending it. I appreciate the suggestion anyway :)
It is a PieceFactory, not a Piece, please have a look at the above code now
Oh that's an interesting solution, I'll try that!
0

In Java, you can't do that as super class does not know how many classes are extending it. The closest you can get to it is by implementing the method like this:

class Piece { public static Piece getInstance(Class<? extends Piece> clazz) throws InstantiationException, IllegalAccessException{ return clazz.newInstance(); } } class Pawn extends Piece { } 

You can then call the method and pass Piece's class like this:

Piece piece = Piece.getInstance(Pawn.class); 

This will give you the instance of Pawn class, you can use it to get the instance of any other class that extends Piece.

P.S. This depends on a couple of factors (e.g. child class having a public constructor, Piece class having access to child class etc)

2 Comments

This seems like the closest to what I wanted so far, great idea!
@OdinThorsen this will also help validating all the Piece implementations to have at least one public constructor as instance won't be created otherwise.
-1

The Piece class is abstract, so you cannot instantiate it. You need to have a concrete implementation of your Piece class.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.