This site requires JavaScript, please enable it in your browser!
Greenfoot back
Zamoht
Zamoht wrote ...

2013/7/5

Classes and inheritance

Zamoht Zamoht

2013/7/5

#
I have two super classes SmoothMover and PathFinder. Then I have three sub classes Bullet, Spawner and Enemy. Bullet is a subclass of SmoothMover and Spawner is a subclass of PathFinder. My problem is that I want Enemy to inherit both SmoothMover and PathFinder. I guess I could either write all the code again in the Enemy class or I could make PathFinder a subclass of SmoothMover and then have the extra methods from SmoothMover in my Spawner class without using them. This just seems like an odd solution, so I would like to know if there is a smoother way to go around this. Please tell me if my post isn't clear.
Gevater_Tod4711 Gevater_Tod4711

2013/7/5

#
To have two classes as superclasses for one subclass is not possible in java (also in any other OOP language I think). Classes can only extend one superclass. But It's possible to implement as many interfaces as you want to use (in this case not so usefull). If you just need the code of the two classes and don't need anything else (what would need the two superclasses) you should just copy the code. Or make one class a subclass of the other class. What is better in your case is up to you.
davemib123 davemib123

2013/7/5

#
I have been reading that there is a multi-inheritance in java: http://en.wikipedia.org/wiki/Multiple_inheritance
Zamoht Zamoht

2013/7/6

#
I don't think Java is on the list of multi-inheritance languages, only JavaFX Script. Well I guess I will go with my own solution (and the one Gevater_Tod4711 said would work), making PathFinder a subclass of SmoothMover and then have some extra useless methods and variables in my Spawner from the SmoothMover.
You need to login to post a reply.