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 it?

Upd . Since this is not possible, how should I use AbstractBillingActivity with settings?

UPD2 . If I use interfaces, I have to create:

  • BillingInterface

     public interface BillingInterface extends PreferenceActivity, AbstractBillingActivity { } 
  • Preference activity

     public interface PreferenceActivity { } 
  • AbstractBillingActivity

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

and then

 public class Preferences implements BillingInterface { 
+60
java android multiple-inheritance
Jul 05 '11 at 19:09
source share
13 answers

Java does not support multiple inheritance.

There are several workarounds that I can think of:

The first is aggregation: create a class that takes these two actions as fields.

The second is the use of interfaces.

The third is to rethink your design: does the Preferences class make sense to be both PreferenceActivity and AbstractBillingActivity ?

+52
Jul 05 '11 at 19:09
source share

Java does not support multiple inheritance. You can implement multiple interfaces, but not extend multiple classes.

+24
Jul 05 2018-11-11T00:
source share

Another solution is to create a private inner class that extends the second class. for example, 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... } } 
+15
Apr 19 '14 at 15:49
source share

Java 1.8 (as well as Groovy and Scala) has a thing called Interface Defender Methods , which are interfaces with predefined method bodies by default. By implementing multiple interfaces that use protection methods, you can effectively, in some way, extend the behavior two interface objects.

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

 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 
+10
Oct 31 '13 at 4:37 on
source share

No, you cannot make a class available for two classes.

A possible solution is to extend it from another class and make a class that extends from another.

+5
Jul 05 2018-11-11T00:
source share

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

The easiest solution is to create an interface for AbstractBillingActivity and PreferenceActivity and implement both of them.

+3
Nov 24 '16 at 21:38
source share

What you are asking is multiple inheritance, and it is very problematic for a number of reasons. Multiple inheritance was specifically prevented in Java; instead, a choice was made to support the implementation of several interfaces, which is a suitable solution.

+1
Jul 05 2018-11-11T00:
source share

Java does not support multiple inheritance, so you cannot extend a class from two different classes at the same time.

Rather, use one class to extend and use interfaces to add additional functions.

0
Jul 05 2018-11-11T00:
source share

Familiar with a multi-level hierarchy?

You can use a subclass as a superclass for your other class.

You can try this.

 public class PreferenceActivity extends AbstractBillingActivity {} 

then

 public class Preferences extends PreferenceActivity {} 

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

0
Apr 19 '18 at 8:22
source share

I can come up with a workaround that can help if the classes you want to extend include only methods.

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

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

0
Feb 09 '19 at 9:37
source share

In addition, instead of inner classes, you can use two 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, you have a person who can have some kind of Phone with his number and type, also you have DeviceInfo for this Phone.

It is also possible to use DeviceInfo as a field in the Phone class, for example

 class Phone { DeviceInfo info; String phoneNumber; Stryng phoneType; //sets; gets } 
0
Mar 21 '19 at 12:42
source share

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

0
Apr 10 '19 at 3:43
source share

In Groovy, you can use trait instead of a class. Since they act similarly to abstract classes (that way, 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 note that this is not exactly the same as extending two classes, but in some cases (for example, in the above example), this may resolve the situation. I highly recommend analyzing your design before moving on to using attributes, they are usually not required, and you cannot implement inheritance nicely (for example, you cannot use protected methods in attributes). Follow the recommendations of the accepted answer, if possible.

0
Aug 26 '19 at 9:02
source share



All Articles