Reputation: 1401
I am struggling with modeling a specific scenario. Let's say we have a super class and three subclasses.
The subclasses share a list of methods from the super class. However, there is one method which is needed to be shared by two out of three classes. So, I thought of two scenarios:
Adding the method in the two subclasses. If I do that though, I will have code duplication since the implementation of the method is same.
Keep the method in the super class to avoid code redundancy based on scenario one above. However, wouldn't that allow the third class to inherit the method? Which defeats the purpose.
So, any ideas on how to model such a scenario ?
Upvotes: 1
Views: 468
Reputation: 36313
This is what I'd come up with:
Just create a sub-class from Main
and add method you need for your both classes.
Upvotes: 0
Reputation: 726779
Neither of your two scenarios is particularly good. The need to inherit a certain method in two out of three classes suggests that you are probably missing an abstract class in the middle, from which two classes inherit:
public class Top {
public void method1() {}
public void method2() {}
}
public abstract class AbstractTwoAndThree extends Top {
public void method3() {}
}
public class Derived1 extends Top {
}
public class Derived2 extends AbstractTwoAndThree {
}
public class Derived3 extends AbstractTwoAndThree {
}
This way, all three Derived
classes inherit method1
and method2
, while method3
is inherited only by Derived2
and Derived3
.
Note: The reason I made the class in the middle abstract
is to stress that it is intended to be inherited, and must not be used by itself. It is not necessary to add any abstract methods to it.
Comment: What if we need to create a sharing graph that is not a tree?
In situations like that you need multiple inheritance for implementation, which is not available in Java. However, multiple inheritance of interfaces is available, which will let you declare the methods that you need only in the classes that must have them. Now you can offload the implementation into a package-private class, or put a default implementation into the interface, depending on the nature of the method:
public class Top {
public void method1() {}
public void method2() {}
}
public interface TwoAndThree {
void method3() {}
}
public class Derived1 extends Top {
}
class TwoAndThreeHelper { // The class is not public
public static void method3impl(TwoAndThree inst) {
... // Implement the method
}
}
public class Derived2 extends Top implements TwoAndThree {
public void method3() {
// Call the shared implementation to avoid duplicating code
TwoAndThreeHelper.method3impl(this);
}
}
public class Derived3 extends Top implements TwoAndThree {
public void method3() {
// Call the shared implementation to avoid duplicating code
TwoAndThreeHelper.method3impl(this);
}
}
Upvotes: 2
Reputation: 883
With pure UML, you are allowed to inherit from multiple classes at once. That is, you may model a second superclass containing that method, from which only the two subclasses may inherit (in addition to the common superclass).
Reference: http://www.uml-diagrams.org/generalization.html#multiple-inheritance
This becomes problematic when implementing your diagram in languages that disallow this, such as Java (where you have to choose either of your two solutions. But for the first solution, you could also add an interface, so that at least the method is declared in a central place). But until then, on a conceptual level, you are fine :)
Upvotes: 0