hello all, im doing a practice project and its a simple text based dungeon crawler. i have a parent class called Player, with two subclasses for diff heroes that the user can select. One is Ninja, one is barbarian. the driver class/main will run, and it will ask for the user to type in play if they wanna play, and here is where im "split." im deciding whether or not to ask for the name of the user or class first. the reason why is that my player class holds the information for the ther player's name, hp, etc, and i was planning on adding the logic to check if it is alive and whatnot. but with inheritance, can i just ask for the hero type first, and THEN create and object of the subclass that i chose, which will automatically give the user the "features" of the parent player class? I guess what I'm trying to ask is that is it perfectly fine to only use the subclass when running the game/main? this is a ramble and a half so feel free to ask for clarifications
#inheritance question/use
1 messages · Page 1 of 1 (latest)
<@&987246399047479336> please have a look, thanks.
Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
You can use inheritance in Java by creating a parent class (Player) and then creating subclasses (Ninja, etc.) for different heroes. This allows you to reuse code and create a hierarchy of classes.
Useful links:
you should avoid inheritance and istead use composition + interfaces
ok yeah, i was making this after i heard the anti inheritance stuff for beginners
well i guess would an example of compistion be having the main Parent class, while having classes dedicated to its stats, class, etc?
or is that not necesarilly composition lol
you want a Player class, and an ClassBehavior interface. the player object wil hold a classbehavior, that will be implemented by several classes
okay i think im understanding. i'll have to refresh myself on interfaces but i think i can see the usefulness of them
would it be good practice to have multiple interfaces?
for modularization?
changed up the design (kind of)
i have the player class, but also an enemy class. both will implement a "behviors/stats" interface that would include hp/attack text/checking if they're dead. but could i make an interface JUST for the stats and one for the behaviors as well
Last time I did something like this, I had no player and no enemy class, only a character class and it just that the player is controlling one of the characters, the other are controlled by bots.
Easy multi-player
it depends if you intend to have several implementations