-1

I have two activities that both of then extends from a base activity.

i.e.

CarActivity{

}

GasCarActivity extends CarActivity{

  function onCreate(){
     registerGasEvent() // so that onLowGasEvent() will work
  }

  function checkGas(){}

  @Subscribe(threadMode = ThreadMode.MAIN)
  function onLowGasEvent(){

  }
}

ElectricCarActivity extends CarActivity{

  function checkBattery(){}

  @Subscribe(threadMode = ThreadMode.MAIN)
  function onLowBatteryEvent(){

  }
}

Now I need a HybridCarActivity that uses both GasCarActivity and ElectricCarActivity functions and life cycle events.

I'm also using eventbus library, how can I have the Subscribe function of both onLowGasEvent and onLowBatteryEvent in the HybridCarActivity?

What I can do here?

CodingTT
  • 1,125
  • 3
  • 14
  • 23

3 Answers3

1

Java doesn't have multiple inheritance. You are going to have to make the Car object have the shared methods that you want to use.

Kristy Welsh
  • 7,828
  • 12
  • 64
  • 106
1

Java does not support multiple inheritance of implementation, but it supports multiple inheritance of interface. So, you can implement as many interfaces as you want. To avoid duplicates and share code between several classes, you can use delegates.

For example:

  1. Interfaces:
interface IGas {
  checkGas();
  onLowGasEvent();
}

interface IBattery
  checkBattery();
  onLowBatteryEvent();
}
  1. implementations of common functionality
class GasImpl implements IGas {
  checkGas() {
  ...
  }
  onLowGasEvent() {
  ...
  }
}

class BatteryImpl implements IBattery {
  checkBattery() {
  ...
  }
  onLowBatteryEvent() {
  ...
  }
}
  1. Activities (pay attention to implementaion setion):
class GasCar extends Car implements IGas {
  IGas mGasImpl = new GasImpl();   // create it in your own code, or inject to inverse dependencies, etc.

  checkGas() {
    mGasImpl.checkGas();
  }

  onLowGasEvent() {
    mGasImpl.onLowGasEvent();
  }
}

class BatteryCar extends Car implements IBattery {
  IBattery mBatteryImpl;   

  checkBattery() {
    mBatteryImpl.checkGas();
  }

  onLowBatteryEvent() {
    mBatteryImpl.onLowBatteryEvent();
  }
}
  1. And then your HybridCar will be something like this:
class HybridCar extends Car implements IGas {
  IGas mGasImpl;
  IBattery mBatteryImpl;   

  checkGas() {
    mGasImpl.checkGas();
  }
  checkBattery() {
    mBatteryImpl.checkBattery();
  }
}
  1. If you want to work with EventBus, you should keep subscribers as an Activity interface - but delegate it's implementation to the handlers as well. So, in the HybridActivity:
  @Subscribe(threadMode = ThreadMode.MAIN)
  function onLowGasEvent(){
     mGasImpl.onLowGasEvent();
  }

  @Subscribe(threadMode = ThreadMode.MAIN)
  function onLowBatteryEvent(){
    mBatteryImpl.onLowBatteryEvent();
  }

If onLow...Event requires Context, update interface methods to pass it as a parameter.

Hope it helps.

S-Sh
  • 3,564
  • 3
  • 15
  • 19
  • Because Activity is special, it comes with activity life cycle events. So how do we inherent that in this case? – CodingTT Apr 09 '19 at 18:43
  • @CodingTT, I'm not sure if I understand you problem fully. Could you provide some example? – S-Sh Apr 09 '19 at 18:50
  • I updated the question, I think that has more info for the question – CodingTT Apr 09 '19 at 19:03
  • @CodingTT, thanks, it's much clearer now. I have very low experience with `EventBus`, but I think, the suggested approach is still possible. Please, review the updated answer – S-Sh Apr 09 '19 at 19:18
0

As @Kristy Whesh suggest, Java doesn't allow multiple inheritance...but there are interfaces!

Just do something like this:

-Create two interfaces: One for gas car and the other for Electric car.

-Your hybrid car implements these two interfaces, so you have the two funcions that you need to ovveride.

-Your old car classes implements the correct interface according to their type.

Hope this help or give you some hint.

Cheers

TomD88
  • 721
  • 6
  • 11