Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple super classes and code reuse

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!

like image 290
vane Avatar asked Mar 12 '13 04:03

vane


People also ask

How can we use inheritance to reuse already written and tested code in programs?

Reusability: Inheritance supports the concept of “reusability”, i.e. when we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class. By doing this, we are reusing the fields and methods of the existing class.

How does inheritance achieve code reuse?

Implementation inheritance is described as a way to achieve code reuse. This works by defining methods and variables in a class that is then inherited by other classes. Those other classes are said to be derived from the original class, and the original class is said to be their base class.

What is reusability of code with example?

A very common example of code reuse is the technique of using a software library. Many common operations, such as converting information among different well-known formats, accessing external storage, interfacing with external programs, or manipulating information (numbers, words, names, locations, dates, etc.)

What is meant by code reusability?

Code reuse is the practice of using existing code for a new function or software. But in order to reuse code, that code needs to be high-quality. And that means it should be safe, secure, and reliable.

How well do you use Java for code reuse?

of the most compelling features about Java is code reuse. code and change it. very well. Like everything in Java, the solution revolves around the class. You you use existing classes that someone has already built and debugged. trick is to use the classes without soiling the existing code.

What is a superclass in Java?

A superclass is the class from which many subclasses can be created. The subclasses inherit the characteristics of a superclass. The superclass is also known as the parent class or base class.

What is the difference between a new class and old class?

The new class contains all the features and functionalities of the old class in addition to its own. The class which is newly created is known as the subclass or child class and the original class is the parent class or the superclass.

What is a subclass of a class?

Subclasses A subclass is a class derived from the superclass. It inherits the properties of the superclass and also contains attributes of its own.


1 Answers

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.

like image 58
G. Blake Meike Avatar answered Oct 02 '22 15:10

G. Blake Meike