I know Java doesn't support multiple inheritance by not allowing to extend more than one class. I just want to know if there is a workaround for my issue.
I've a class named CustomAction
which needs to extend two abstract classes, BaseAction
and QuoteBaseAction
. I can't change any of these abstract classes and make one extend other.
These abstract classes have method implementations which I need to make use of. I can't create an interface also since an interface can extend only another interface.
Any ideas?
You can only Extend a single class. And implement Interfaces from many sources. Extending multiple classes is not available.
The reason behind this is to prevent ambiguity. Consider a case where class B extends class A and Class C and both class A and C have the same method display(). Now java compiler cannot decide, which display method it should inherit. To prevent such situation, multiple inheritances is not allowed in java.
A class can extend only one class, but implement many interfaces.
Although classes can inherit only one class, they can implement multiple interfaces.
Here you go ...
public class CustomAction extends BaseAction {
class Inner extends QuoteBaseAction {
}
}
Composition is the way to go.Make one of your abstract class as part of your class.
public abstract class Abs1 {
//
}
public abstract class Abs2 {
//
}
public class Main extends Abs1 {
Abs2 abs2 = ...
//impl
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With