I was studying for my Intro to Java I exam and I don't think I need to get this far into inheritance but it made me curious. I decided to practice with Pokémon because even though most of the examples are for animals I wanted to make it fun. But this brought up an issue for when I wanted to have a Pokémon with multiple types. (I am using BlueJ in the screenshot because its what our teacher forces us to use. vv)
At first I just made a class for "Pokémon" which had all of the necessary info and stats variables and then extended it to different types so that I could have all that info and also set their weaknesses and strengths.
I was just going to choose two random Pokémon to practice with (thus the Bulbasaur and Pikachu) and everything lines up then but I thought to add one that was both their types to see what I could do with that but I couldn't make the new Pokémon (Rotom-Mow) have both the Grass type and Electric type class. I tried to make a separate class that combined both types too but I don't know why that would work if trying to put them on the actual Pokémon wouldn't. I expected it to, for example, carry everything over from both the Pokémon class as well as the Grass and Electric types into Rotom-Mow.
I know I could not have separate classes for the types and just know which types each Pokémon was then put them in manually but that's the easy way out and also I would rather not have to type it for each new Pokémon. I guess along a similar vein I could just have lists stored of different types weaknesses and strengths and call upon those?
Also what if two Pokémon had a child with traits from both parents that seems like a thing I should be able to do as well.
But I want to know what you guys would do to either inherit multiple classes or solve my problem. I'm sure there is an obvious solution to seasoned programmers but before learning Java for this semester all I had done programming wise was Scratch when I was little then Python which isn't object based.
TL;DR- How can I have a class inherit multiple kinds of classes that are unrelated to one another linearly? (All sort of on the same "tier" of the cake I guess) Ex. a Pokémon that is both a Grass type and Electric type.
No, but, you're thinking about it the wrong way.
The two plausible ways to represent, say, Bulbasaur, is either:
Or possibly:
Where as additional upgrades you might want to turn those types into an
enuminstead.Why?
Generally introducing types is a bad idea unless the type brings new functionality. It's fairly obvious that
Pokemonas a type brings various methods along with it - any Pokemon has agetName()method, for example.However, what would
GrassTypebring? Not, necessarily,public void leafBlade(Pokemon target) {}, because not all grass type pokemon have leaf blade, and some pokemon with leaf blade aren't grass type.So, 'grass type' adds no new methods to the table, and it is therefore generally a bad idea to make it a type, then. It'd be a type that defines nothing.
Generally when you write
X extends Yyou need to ensure that Y is the only obvious conceptual parent type of the thing. Bulbasaur is, primarily, a pokemon. It is not 'primarily' a 'GrassType'. It cannot be; after all it cannot both be primarily a GrassTypePokemon and a PoisonTypePokemon. Literally, in java's case: You can only extend one class.Possibly, 'Bulbasaur' shouldn't be a class either. Is there anything in particular that all Bulbasaurs imaginable can do, which no other Pokemon could? I don't think any such thing exists. There is no attack that guaranteed every bulbasaur has, for example. Most likely you just want a
Pokemonclass that has no subclasses.Generally, convoluted type hierarchies are a bad idea. Introduce them sparingly, and only if they add something useful - if the various types you define come with new methods that are universally applicable. You can't 'undeclare' a method in java, if you
extends GreeterandGreeterdefines avoid hello()method, then.. you have an hello method. You can't get rid of it. It's nasty to have to implement it asthrow new UnsupportedOperationException("Even though I said I was a greet, this one cant speak");- that's a code smell.