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:
BillingInterface
public interface BillingInterface extends PreferenceActivity, AbstractBillingActivity { }
PreferenceActivity
public interface PreferenceActivity { }
AbstractBillingActivity
public interface AbstractBillingActivity { void onCreate(Bundle savedInstanceState); }
and then
public class Preferences implements BillingInterface {
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
andPreferenceActivity
and implement them both.Also, instead of inner classes, you can use your 2 or more classes as fields.
For example:
}
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
Another solution is to create a private inner class that extends the second class. e.g a class that extends
JMenuItem
andAbstractAction
:Java doesn't support multiple inheritance. You can implement multiple interfaces, but not extend multiple classes.
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.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: