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 8159487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T17:55:53+00:00 2026-06-06T17:55:53+00:00

I have had several situations when i would like to do that. This could

  • 0

I have had several situations when i would like to do that. This could be an example:

Consider a possible implementation for the chess game. We define the abstract class ‘Piece’ and classes inheriting from it: ‘bishop’ ‘peon’ ‘tower’ ‘horse’ ‘queen’ etc

We may have our peon about to reach the end of the board, and it may be required for a method called onto that peon object to change the class of that object to ‘Queen’, ‘horse’ or whatever.

So my question is, is there any way to do that in C++ or Java? If not, in any other language?
Also, are there other approaches for this situations in general?

  • 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-06-06T17:55:56+00:00Added an answer on June 6, 2026 at 5:55 pm

    You might be able to use the decorator pattern or the strategy pattern.

    With decorator:

    interface ChessPiece {
        Set<Move> getPossibleMoves();
        Image getAppearance();
    }
    
    class Pawn implements ChessPiece {
        ...
    }
    
    class Decorator implements ChessPiece {
        ChessPiece decorated;
    
        public Decorator(ChessPiece decorated_) {
            this.decorated = decorated_;
        }
    
        @override
        Set<Move> getPossibleMoves() {
            Set<Move> result = decorated.getPossibleMoves();
            // alter result
            return result;
        }
    }
    

    When needed you replace the Pawn instance with the Decorated instance. So it still involves replacing one instance with an other as mentioned in OpenSause’s answer.

    ChessPiece pawn = new Pawn;
    ...
    pawn = new Decorated(pawn);
    

    When you plan ahead you can use strategy it is still not really changing classes:

    interface PossibleMovesStrategy {
        Set<Move> getPossibleMoves();
    }
    
    interface AppearanceStrategy {
        Image getAppearance();
    }
    
    class ChangingChessPiece extends ChessPiece {
        PossibleMovesStrategy posMoves;
        AppearanceStrategy appearance;
    
        @override
        Set<Move> getPossibleMoves() {
            return posMoves.getPossibleMoves();
        }
    
        @override
        Image getAppearance() {
            return appearance.getAppearance();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my limited experience, I've been on several projects that have had some sort
I have recently had several situations where I need different data from the same
I have tried several things so far, but I haven't had much luck yet.
I have had this problem crop up a few times and I can't figure
I have had a few problems getting this right, so I wanted to ask
I have noticed that when multiple attributes of a Backbone model are set like
We have had several problems with being hacked. We currently have all the config
I have several databases that each have a table called, say, products . One
I have had several occasions recently to access a specific class several times over
I have several controls in a winform application that I added databindings to using

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.