Reputation: 20409
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 {
Upvotes: 80
Views: 336364
Reputation: 1118
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
}
Upvotes: 0
Reputation: 25200
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.
Upvotes: 0
Reputation: 11
If you are interested in using methods from multiple classes, the best way to approach to it is to use Composition instead of Inheritence
Upvotes: 0
Reputation: 14998
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/
Upvotes: 0
Reputation: 386
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.
Upvotes: 1
Reputation: 649
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 PreferenceActivity
and implement them both.
Upvotes: 3
Reputation: 313
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...
}
}
Upvotes: 18
Reputation: 29669
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
Upvotes: 11
Reputation: 29166
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.
Upvotes: 0
Reputation: 39480
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.
Upvotes: 1
Reputation: 7223
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.
Upvotes: 6
Reputation: 206689
Java doesn't support multiple inheritance. You can implement multiple interfaces, but not extend multiple classes.
Upvotes: 27
Reputation: 36852
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
?
Upvotes: 71