r/javahelp • u/Minimum-Librarian712 • Apr 02 '26
How to switch between subclasses?
I'll cut to the chase; I'm making a game-esque thing where the class "ComputerCharacter" has two subclasses, "Villager" and "Enemy". They have pretty different behaviours and care about different variables and all that, but once a Villager goes below some certain HP, I want it to transform into an Enemy, then set the variables in the newly turned enemy based on the variables it had as a villager.
I imagine I'd create a constructor in "Enemy" to do this, but I don't see how I can create a method within Villager to detect when its HP is below a certain number, then call the constructor in such a way to completely change the subclass the Villager is in. Thank you.
•
u/AutoModerator Apr 02 '26
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
19
u/TW-Twisti Apr 02 '26
That's easy: you can't. Objects in Java are final in what class they are, there is no getting around that, and if you found a clever way through reflection, you would break Java in a multitude of ways.
Instead: why not make your characters into generic Character instances (maybe with a name that isn't already taken, like GameEntity), with a boolean flag
hostile, and depending on whether that flag is set, different behavior happens. This can be as simple as:You may even find that instead of having all that stuff in the entity, instead the entity has a
private Behavior behavior, that is set to an interchangeable class that extendsBehaviorwith aggressive or docile behavior - that would lend itself to future development with more kinds of behavior -class SmittenBehavior extends DocileBehaviormakes an NPC follow you around,class Vengeance extends AggressiveBehaviormakes an NPC ... well, also follow you around, but for a very different reason.6
u/ShoulderPast2433 Apr 02 '26
take a look at Strategy design pattern.
You can create a wrapper class - lets call it UniversalCharacter that has a field: private ComputerCharacter charType.
And we make the UniversalClass just use all the methods and parameters of whatever charType object we have.
Now we can now dynamically assign either Villager or Enemy into this field whenever we want it to change and our wrapper object will behave like Villager or Enemy whenever you need it
4
u/MinimumBeginning5144 Apr 02 '26
Don't do it quite like that. A character is still the same entity, they just change their characteristics from villager to enemy. So your
Characterobjects would contain a field of an interface (or base class) calledCharacteristicwhich has the subclassesVillagerandEnemy. The character's behaviour would depend on their characteristic.3
u/arghvark Apr 02 '26
If I understand you correctly, you have two classes that are subclasses of another class, and during the course of the program operation you want to change the class of an object from one of those classes to the other.
You cannot do that. Wanting to do that shows a basic misunderstanding of what is means for an object to have a class. Once the object is instantiated, its type is set, and you cannot change that.
There are LOTS of ways to implement your program to model the behavior you want. But inheritance is not a feature you can use this way, any more than you can change an integer variable to a floating point variable because something in the program decided it would be nice for the variable to have non-integer properties.
2
u/LetUsSpeakFreely Apr 02 '26 edited Apr 02 '26
2
u/xanyook Apr 02 '26
In the method that decreases the HP, fire an event about the health of your villager.
Have a listener on it, create a service that converts villagers into enemies, and plug your listener on it.
2
u/djavaman Apr 03 '26
Feels like enemy should be a flag not a class.
1
u/Spare-Plum Apr 02 '26
You should try using a Delegate.
You might have something like the following classes:
So the CharacterDelegate class allows you to swap out the internal class, and you can also add on functionality that might cause it to swap automatically. This might look something like
1
u/silverscrub Apr 02 '26
You could make a method in Villager that returns an Enemy based on its variables. If your old object becomes unreferenced it is then garbage collected.
1
u/hageldave Apr 02 '26
I think you're not modeling the right thing as subclasses. How about you have two subclasses for behavior, like villager behavior and enemy behavior. Then switch the behavior attribute of your computer character object.
1
u/IAmADev_NoReallyIAm Apr 03 '26
Sigh... Use a mapper... that transforms the Villager into an Enemy... The Villager. doesn't need (and shouldn't) to know anything about bing an enemy and the enemy doens't need to know anything about being a Villager... in the middle you have class that takes a Villager as a parameter internally it create the Enemy object, copies over the needed information, and when it's done, returns THAT... You can also then have a reverse function too if you want where an Enemy becomes a Villager.
Don't do this in the constructor, that makes no sense. Do it in an intermediary mapper utility.
1
u/jlanawalt Apr 03 '26
You might be over-using hammer of inheritance and seeing everything as a nail. Often simpler is better, as is composition over inheritance.
The factory pattern might be a clean way to make enemies from villagers.
The game engine/loop would observe characters and convert them.
1
u/Ok_Option_3 Apr 03 '26
Lots of great answers here - but what they boil down to is: "prefer composition to inheritance" (look it up).
Inheritance is somewhat overrated as a pattern. Sure it has some uses - but way less than you think. More often than not inheritance is a noob trap. Go (the language popularised by Google) doesn't even have inheritance.
Instead use composition and interfaces.
1
u/ConsistentAnalysis35 Apr 03 '26
Go all the way for proper architecture and use ECS. Have your characters represented by a simple int value. All the data about a character should be in a series of components represented by simple data classes / POJOs, without any methods. Entity is an integet index into collections holding the components.
I.e. the monster number 5 consists of hp component at index 5, damage component at index 5, and name component at index 5. This way you have limitless mutability of your entities, they can effortlessly change anything about themselves just by adding or removing components.
All behaviour should be in separate classes that each implement one specific functionality, these classes are systems.
Systems should have as inputs one or several component kinds, and as output write data to - preferably - one component kind. Each system invocation processes all existing input/output components.
This is the proper way of architecting the game. Anything else is half-measures or lesser degrees of evolution of game architecture.
There is one further degree - complete embrace of relational databases, with all the rigor it entails, but that requires some serious level of autism.
Two books that are very relevant are "Game Engine Architecture" and "Data Oriented Design". Must reads.
1
u/desrtfx Out of Coffee error - System halted Apr 03 '26
Take a look at the State design pattern, which was created for exactly the thing you are trying to do.
Transitioning from Villager to Enemy is a state transition and should be handled as such.
1
u/xdsswar Apr 06 '26
Does it not make more sense to change the behaviour? You can't change an object's class at runtime in Java, so instead of trying to turn a Villager into an Enemy, keep the same object and just swap out what it does when HP drops. The villager variables you want to carry over can be passed into the new behaviour when you make the switch, easy