#inheritance question/use

1 messages · Page 1 of 1 (latest)

covert jasper
#

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

rustic ironBOT
#

<@&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>.

#
TJ-Bot
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 wi

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:

  1. Oracle's Java Tutorials on Inheritance
  2. GeeksforGeeks article on Inheritance in Java
  3. Tutorialspoint's Java Inheritance tutorial
bleak falcon
covert jasper
#

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

bleak falcon
covert jasper
#

okay i think im understanding. i'll have to refresh myself on interfaces but i think i can see the usefulness of them

covert jasper
#

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

elfin coral
#

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

bleak falcon