[java] Can one class extend two classes?

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 {

This question is related to java android multiple-inheritance

The answer is


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

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;
Stryng phoneType;
//sets; gets
}

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/


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


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.


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


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.


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.


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.


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.


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...
    }

}

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.


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to android

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How to implement a simple scenario the OO way My eclipse won't open, i download the bundle pack it keeps saying error log getting " (1) no such column: _id10 " error java doesn't run if structure inside of onclick listener Cannot retrieve string(s) from preferences (settings) strange error in my Animation Drawable how to put image in a bundle and pass it to another activity FragmentActivity to Fragment A failure occurred while executing com.android.build.gradle.internal.tasks

Examples related to multiple-inheritance

Java Multiple Inheritance Can a normal Class implement multiple interfaces? Can an interface extend multiple interfaces in Java? Class extending more than one class Java? Can one class extend two classes? Multiple inheritance for an anonymous class How does Python's super() work with multiple inheritance? Does C# support multiple inheritance? What is a mixin, and why are they useful? What does 'super' do in Python?