Various class types inherit from abstract class in android java

I have two classes (ClassA and ClassB) that should be a ListFragment. These two classes share behavior, so I created an abstract class (AbstractClass). Therefore AbstractClass inherits from ListFragment. So far I have classes ClassA and ClassB that inherit from my AbstractClass that inherit from ListFragment. The problem arises when I need another ClassC, which should be a Fragment (not a ListFragment), but also need the behavior from AbstractClass, but I cannot inherit it because ClassC has to be just a Fragment and my abstractClass is a ListFragment. I would like my ClassA, ClassB and ClassC to inherit from my AbstractClass, but there are ClassA, ClassB Listfragment and ClassC only Fragment. I'm sure,that it should be some kind of Java development pattern or good practice to solve this problem.

I know java doesn't allow multiple inheritance.

Many thanks

+3


source to share


2 answers


Can you extract common functionality for some helper class? Use composition instead of inheritance



+1


source


Create a BaseAbstractFragment class that will be abstract and inherit from Fragment. Now create another ListViewAbstractClass (a subclass of BaseAbstractFragment) and add a ListView to it instead of inheriting from ListFragment. Now subclass ClassA and ClassB from ListViewAbstractClass and ClassC from BaseAbstractFragment.



0


source







All Articles