I have the following scenario and I'm not sure how best to avoid having copy and paste code:
Class A {
}
Class B extends A {
}
Class C extends A {
}
Class D extends B{
}
Classes A and B are part of a library that I can't change. Unfortunately, Class C and Class D are identical in every singe way. The fields are the same and the methods are the same with the same implementations. The only difference is that one extends class A and the other extends Class B.
I then have lots of classes that extend C or D throughout my program. The question is: is there any way to avoid having to copy and paste code from class C into class D? I have considered using an interface which C and D implement since that seemed like the obvious solution, however I need to have some of the fields and methods in C and D be private/protected which I can't do with an interface.
They can, however I think this will still leave me having to either write all the fields out twice, or making them public within the class that has been delagated to. Unfortunately, I really need these fields to be protected.
Do all the classes really need to extend C or D? Can't you just use composition?
Inheritance is a powerful tool, but far too easily abused. Can you tell us the actual class names for A, B, C and D? This will help in determining if inheritance really is the solution.
Eliminate 95% of the weeds in your lawn by mowing 3 inches or higher. Then plant tiny ads:
a bit of art, as a gift, that will fit in a stocking