80

My class should extend two classes at the same time:

public class Preferences extends AbstractBillingActivity {

public class Preferences extends PreferenceActivity {

How to do so?

Upd. Since this is not possible, how should I use that AbstractBillingActivity with Preferences then?

Upd2. If I go with interfaces, should I create:

  1. BillingInterface

    public interface BillingInterface extends PreferenceActivity, AbstractBillingActivity {
    
    }
    
  2. PreferenceActivity

    public interface PreferenceActivity {
    
    }
    
  3. AbstractBillingActivity

    public interface AbstractBillingActivity {
    
            void onCreate(Bundle savedInstanceState);
    
    }
    

and then

public class Preferences implements BillingInterface {
Asad Rasheed
  • 518
  • 5
  • 13
LA_
  • 19,823
  • 58
  • 172
  • 308
  • You can't: http://www.javaworld.com/javaworld/javaqa/2002-07/02-qa-0719-multinheritance.html – James Allardice Jul 05 '11 at 19:10
  • 1
    @LA_: A class can implement multiple interfaces but extends only one class, so you can implement your class with all your interfaces without merging all into one interface, if you want. – Asad Rasheed Jul 05 '11 at 20:23
  • Once you have created AbstractBillingActivity and PreferenceActivity as interfaces, you don't need to create another interface named BillingInterface. Instead use this code: public class Preferences implements AbstractBillingActivity,PreferenceActivity – Logan Jul 06 '11 at 04:44

13 Answers13

71

Java does not support multiple inheritance.

There are a few workarounds I can think of:

The first is aggregation: make a class that takes those two activities as fields.

The second is to use interfaces.

The third is to rethink your design: does it make sense for a Preferences class to be both a PreferenceActivity and an AbstractBillingActivity?

Etienne de Martel
  • 34,692
  • 8
  • 91
  • 111
  • Interfaces don't work because they are interfaces...they only support public methods. See the problem discussed here: https://stackoverflow.com/questions/5376970/protected-in-interfaces – Hasen May 01 '21 at 02:40
  • To be honest, the question was vague enough when it was asked _nearly 10 years ago_ that at the time it made sense to suggest using interfaces. But even today, I still don't see why interfaces wouldn't work, or, rather, why you would absolutely need protected methods here. – Etienne de Martel May 01 '21 at 17:27
27

Java doesn't support multiple inheritance. You can implement multiple interfaces, but not extend multiple classes.

Mat
  • 202,337
  • 40
  • 393
  • 406
  • how should I use that AbstractBillingActivity (https://github.com/robotmedia/AndroidBillingLibrary) with Preferences then? – LA_ Jul 05 '11 at 19:12
  • 1
    I don't know. At first glance it doesn't make sens to want to extend both something called `Preferences` and `BillingActivity` - I don't see any link between these two. Use composition if your preferences need to store an billing activity thing or vice-versa. – Mat Jul 05 '11 at 19:15
  • where can I read about composition? This is something new for me. Or, could you please post an example? Thank you. – LA_ Jul 05 '11 at 19:18
  • [Composition](http://en.wikipedia.org/wiki/Object_composition) is just having an object as a member of another one. – Mat Jul 05 '11 at 19:20
18

Another solution is to create a private inner class that extends the second class. e.g a class that extends JMenuItem and AbstractAction:

public class MyClass extends JMenuItem {


    private class MyAction extends AbstractAction {
        // This class can access everything from its parent...
    }

}
Niklas Higi
  • 2,188
  • 1
  • 14
  • 30
Jonny Rimkus
  • 313
  • 1
  • 4
  • 6
11

Java 1.8 (as well as Groovy and Scala) has a thing called "Interface Defender Methods", which are interfaces with pre-defined default method bodies. By implementing multiple interfaces that use defender methods, you could effectively, in a way, extend the behavior of two interface objects.

Also, in Groovy, using the @Delegate annotation, you can extend behavior of two or more classes (with caveats when those classes contain methods of the same name). This code proves it:

class Photo {
    int width
    int height
}    
class Selection {
    @Delegate Photo photo    
    String title
    String caption
}    
def photo = new Photo(width: 640, height: 480)
def selection = new Selection(title: "Groovy", caption: "Groovy", photo: photo)
assert selection.title == "Groovy"
assert selection.caption == "Groovy"    
assert selection.width == 640
assert selection.height == 480
djangofan
  • 28,471
  • 61
  • 196
  • 289
  • 3
    Addendum: I retract this answer: I have since learned that "object composition" is probably what you want to use if you need behavior similar to multiple inheritance. Also, implementing multiple interfaces would not be like multiple inheritance: You cannot have a class that implements two interfaces where both the interfaces contain a default method with the same signature unless the class provides an implementation for that method itself; therefore we still effectively have single inheritance. Always learning... – djangofan Feb 09 '17 at 02:27
6

No you cannot make a class extend to two classes.

A possible solution is to make it extend from another class, and make that class extend from another again.

Jules
  • 7,148
  • 6
  • 26
  • 50
3

Java does not support multiple inheritance. However, your problem may be solved using interfaces.

The easiest solution would be to create an interface for AbstractBillingActivity and PreferenceActivityand implement them both.

nejckorasa
  • 589
  • 3
  • 8
  • 17
1

Familiar with multilevel hierarchy?

You can use subclass as superclass to your another class.

You can try this.

public class PreferenceActivity extends AbstractBillingActivity {}

then

public class Preferences extends PreferenceActivity {}

In this case, Preferences class inherits both PreferencesActivity and AbstractBillingActivity as well.

AyukNayr
  • 386
  • 2
  • 21
1

What you're asking about is multiple inheritance, and it's very problematic for a number of reasons. Multiple inheritance was specifically avoided in Java; the choice was made to support multiple interface implementation, instead, which is the appropriate workaround.

Paul Sonier
  • 38,903
  • 3
  • 77
  • 117
  • I have one question for you.. I think Multiple Inheritance exist in java. Let me explain it Class A Class B. Class A extend the Class B and Class A also extend the Object Class. so how we can say java not support the Multiple inheritance.. – Z.I.J Nov 20 '15 at 10:50
0

I can think of a workaround that can help if the classes you want to extend include only methods.

Write these classes as interfaces. In Java, you can implements any number of interfaces, and implement the methods as default methods in the interfaces.

https://www.geeksforgeeks.org/default-methods-java/

CrazySynthax
  • 13,662
  • 34
  • 99
  • 183
0

Also, instead of inner classes, you can use your 2 or more classes as fields.

For example:

Class Man{
private Phone ownPhone;
private DeviceInfo info;
//sets; gets
}
Class Phone{
private String phoneType;
private Long phoneNumber;
//sets; gets
}

Class DeviceInfo{

String phoneModel;
String cellPhoneOs;
String osVersion;
String phoneRam;
//sets; gets
}

So, here you have a man who can have some Phone with its number and type, also you have DeviceInfo for that Phone.

Also, it's possible is better to use DeviceInfo as a field into Phone class, like

class Phone {
DeviceInfo info;
String phoneNumber;
String phoneType;
//sets; gets
}
musicformellons
  • 12,283
  • 4
  • 51
  • 86
Dred
  • 1,076
  • 8
  • 24
0

If you are interested in using methods from multiple classes, the best way to approach to it is to use Composition instead of Inheritence

Santhosh
  • 11
  • 1
0

In Groovy, you can use trait instead of class. As they act similar to abstract classes (in the way that you can specify abstract methods, but you can still implement others), you can do something like:

trait EmployeeTrait {
    int getId() {
         return 1000 //Default value
    }
    abstract String getName() //Required
}

trait CustomerTrait {
    String getCompany() {
        return "Internal" // Default value
    }
    abstract String getAddress()
}

class InternalCustomer implements EmployeeTrait, CustomerTrait {
    String getName() { ... }
    String getAddress() { ... }
}

def internalCustomer = new InternalCustomer()
println internalCustomer.id // 1000
println internalCustomer.company //Internal

Just to point out, its not exactly the same as extending two classes, but in some cases (like the above example), it can solve the situation. I strongly suggest to analyze your design before jumping into using traits, usually they are not required and you won't be able to nicely implement inheritance (for example, you can't use protected methods in traits). Follow the accepted answer's recommendation if possible.

lepe
  • 24,677
  • 9
  • 99
  • 108
0

Java does not support multiple inheritance, that's why you can't extend a class from two different classes at the same time.

Rather, use a single class to extend from, and use interfaces to include additional functionality.

MD Sayem Ahmed
  • 28,628
  • 27
  • 111
  • 178
  • could you please check my question, upd2? I never worked with interfaces before. – LA_ Jul 05 '11 at 19:34