Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6730545
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:25:16+00:00 2026-05-26T10:25:16+00:00

I’m making a chess game, and I have a base ‘piece’ interface that I

  • 0

I’m making a chess game, and I have a base ‘piece’ interface that I want all the pieces to implement. I have some common variables that each need to have, but I don’t want to make getters and setters in each class (seems like it goes against DRY anyway).

What I’ve done is called my interface PieceBase, have a concrete class called Piece, and I have my actual pieces extent from Piece and implement PieceBase. I don’t want Piece to ever be instantiated though, because it doesn’t make sense by itself, and something about this method makes me uneasy.

How is this normally done?

Specifically, this is in Java, but I don’t know if it’s different in any other OO language.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T10:25:17+00:00Added an answer on May 26, 2026 at 10:25 am

    It’s generally considered bad practice, at least in Java and C++, to use inheritance for the purpose of code reuse (see Joshua Bloch’s “Effective Java” for arguments). It’s typically better to use composition (or some other form of delegation) instead.

    In your case I’d turn things around entirely, such that Piece is a final class simply taking an enum representing the type of piece it is:

    public final class Piece {
    
        public static enum Type {
            PAWN {
                List<Move> getLegalMoves(Color c, Location l) { /* ... */ }
            },
            BISHOP { /* ... */ },
            KNIGHT { /* ... */ },
            ROOK { /* ... */ },
            QUEEN { /* ... */ },
            KING { /* ... */ }; // i hope i didn't forget anything :P
    
            abstract List<Move> getLegalMoves(Color c, Location l);
            // ... etc.
        }
    
        private final Type type;
        private final Color color;
        private Location location;
    
        public Piece(Type type, Color color, Location location) {
            this.type = type;
            this.color = color;
            this.location = location;
        }
    
        public List<Move> getLegalMoves() {
            return type.getLegalMoves(color, location);
        }
    
        // ... etc.
    }
    

    So all of the logic common across all pieces is implemented once in Piece, and the differing portions are encapsulated by the Type. And if you ever need to know what kind of piece you’re looking at, you don’t need to use instanceof and cast or anything, you just implement Piece.getType() and switch on that.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.