2

The MainActivity of the Android game that I am writing needs to extend PApplet, a class inherited from the Processing library, on which the entire game is based:

public class MainActivity extends PApplet{
//code that can use PApplet methods, but not BaseGameActivity ones.
}

Now, I would like to implement Google Play Services for achievements and leaderboard using the BaseGameUtils library.

To use the BaseGameUtils library, my MainActivity needs to extend BaseGameActivity, a class inherited from the BaseGameUtils library:

public class MainActivity extends BaseGameActivity{
//code that can use BaseGameActivity methods, but not PApplet ones.
}

This, however, does not work, since java does not support multiple inheritance:

public class MainActivity extends PApplet extends BaseGameActivity{
} //INVALID!

Therefore, I would like to know of any alternatives you can think of, since I really need to use methods from both libraries. I tried declaring PApplet and BaseGameActivity objects and using their methods, but it did not work. I tried creating a separate class that would extend BaseGameActivity and then instantiate it from MainActivity and use its methods inherited from BaseGameActivity, but it didn't work. I tried other ways (which might not have made sense, but you know, trial and error...), but it didn't work.

I am sure there must be a workaround, but after having used all the resources that main brain has, I still couldn't find any. Therefore, I am asking you for help.

Thank you in advance!

bkr879
  • 2,035
  • 2
  • 15
  • 23
  • 1
    This is a downside of using Processing. Processing has to be its own activity, not just a view, so stuff like this is non-trivial. – Kevin Workman Feb 11 '15 at 13:53

1 Answers1

0

You can create a class which wraps an instance of another class and provides methods which just pass through to the contained instance. In Java 8 you can also provide default method implementations on interfaces, and you can implement multiple interfaces, allowing for a form of multiple inheritance.

The fact that you just want to use the methods of BaseGameActivity means that that would be appropriate to wrap.

Multiple inheritance has a problem called the diamond problem. When you have multiple inheritance and you inherit from two parents which share a common superclass and both parents override the same superclass method, which overridden version do you use when you call that method?

Matthew Franglen
  • 4,441
  • 22
  • 32