Reputation: 2215
This question is related to Android in that my need exists in that domain but the question still applies to Java as a whole; I will be using some Android terms here such as Activity
, FragmentActivity
, ListActivity
, etc...
I need to implement an abstract base class that contains functionality which must be used throughout the whole application, more specifically every Activity
must use this functionality but I want it to be handled automatically by the base class. My problem is that I have many different types of Activities within the application like Activity
, FragmentActivity
and ListActivity
, all of which extend Activity
(minus Activity
of course).
Since the code in the base class would be exactly the same for each implementation of Activity
, is there a way to avoid code duplication and needing to create a base class for each type of Activity
?
What I'm trying to avoid:
public abstract class BaseActivity extends Activity
{
public void onCreate(Bundle savedBundle)
{
// code goes here, will be the exact same for all these base classes
}
}
public abstract class BaseFragmentActivity extends FragmentActivity
{
public void onCreate(Bundle savedBundle)
{
// code goes here, will be the exact same for all these base classes
}
}
public abstract class BaseListActivity extends ListActivity
{
public void onCreate(Bundle savedBundle)
{
// code goes here, will be the exact same for all these base classes
}
}
public class MyMainActivity extends BaseActivity
{
}
public class MyUserList extends BaseListActivity
{
}
public class SomeActivityThatNeedsToBeAFragment extends BaseFragmentActivity
{
}
I was trying to utilize generics for this by using something like the following
public abstract class BaseActivity<T extends Activity> extends T
but obviously that won't work; or maybe I'm just doing it wrong. So is there any way to accomplish this code reuse and avoid duplication or am I just simply stuck duplicating my code?
Thanks!
Upvotes: 11
Views: 1770
Reputation: 6705
Frequently, using Fragments can help with this kind of problem. In your particular case, though I see that you want to use ListActivity, as well. That makes it harder
As you know, Java can't help you here. You can't inherit multiple implementations (List and MyBase). There is no simple answer. I can suggest two things:
List(Activity,Fragment) are pretty simple extensions to their base classes. You could, with a reasonable amount of work, build your own, that inherit from your base class. They would probably be simpler than the ones in the framework. Maybe that would be ok.
Delegate. Write a class that implements the common behavior but that does not inherit from Activity/Fragment. Create one of these objects during the initialization of your Fragment/Activity classes. Any time there is common behavior, delegate it to the delegate instance.
Upvotes: 3
Reputation: 106508
Create an abstract class called BaseActivity
. Implement onCreate()
for it, which should cover the basics of activity operations. Create other classes that extend from BaseActivity
, and allow them to override that method if and only if they do something slightly more special than BaseActivity
.
The main idea here is that whatever Activity
you have or create relates to one another in some way that they do similar code operations. Only in special cases would you need to do something extra, hence overriding onCreate()
.
Upvotes: 0