Alternative of Multiple inheritance in Java
use interfaces:
interface InfoMessAware {
String getMessage();
}
interface BackPageAware {
String getBackPage();
}
class MyBean implements InfoMessAware, BackPageAware {
String getMessage() {
return "message";
}
String getBackPage() {
return "backPage";
}
}
then replace instanceof with standard method calls.
Just to clarify my comment.
Just like Darth Eru says you create the two interfaces and the two default implementations. When you have a bean that needs both of the behaviours you have that class implement the two interfaces but you also create variables of the default implementations. This way you still dont need to duplicate any code.
interface InfoMessAware {
String getMessage();
}
interface BackPageAware {
String getBackPage();
}
class DefaultInfoMessAware {
String getMessage() {
return "message";
}
}
class DefaultBackPageAware {
String getBackPage() {
return "backPage";
}
}
class MyBean implements InfoMessAware, BackPageAware {
private InfoMessAware messAware = new DefaultInfoMessAware();
private BackPageAware backPageAware = new DefaultBackPageAware();
String getMessage() {
return messAware.getMessage();
}
String getBackPage() {
return backPageAware.getBackPage();
}
}
The problem you are describing begs the usage of composition, not inheritance. The class being BackPageAware means it knows about that class/functionality. Inheritance means it IS a BackPage. You have described a HAS A relationship.
As has been said many times now, use interfaces to define the contracts for retrieving the information that the object HAS.