0

I have an abstract class ClassA which derives from ViewGroup.

I would like to extend ClassA to these class :

ClassB with LinearLayout layout logic

ClassC with RelativeLayout layout logic

Is this possible ?

I didn't found how to implement default LinearLayout or RelativeLayout logic in derived method onLayout() and onMeasure().

Is there any other solution if it's not possible ?

olivier_sdg
  • 1,538
  • 1
  • 10
  • 10

1 Answers1

2

I think it would be better to extend LinearLayout and RelativeLayout directly from ClassB and ClassC. Then, you use composition to enhance those classes' behavior with the ClassA's logic.

Cristian
  • 198,401
  • 62
  • 356
  • 264
  • Ok, but with this solution I can't use `ClassA` as parameter type to access derived-method. Not so good – olivier_sdg May 04 '12 at 15:42
  • Yes, you can... that's what delegation is for. By the way, in this case `ClassA` should not extend `ViewGroup` – Cristian May 04 '12 at 15:52
  • Ok, if I understand, I can access `ClassA` methods using `ClassA` instance in `ClassB` and `ClassC`. Right ? But this is not possible to cast `ClassA` to `View`, which cause some problem with my implementation. It seems to be more complex than I was thinking – olivier_sdg May 09 '12 at 10:01
  • Yes, you cannot cast from `ClassA` to `View`... but you can cast from `ClassB` and `ClassC` to `View`. – Cristian May 09 '12 at 13:59