(I'm a novice developer still learning best practices and java - any help/direction would be appreciated, thank you!)
Suppose I have the following situation:
ClassA
extends abstract ClassB
which extends abstract ClassC
.ClassD
extends abstract ClassE
which extends abstract ClassF
which extends abstract ClassC
.[ClassC
is the parent class]
where ClassA
and ClassD
have a set of identical properties, getters, & setters.
I want to avoid that code duplication (properties,getters,setters). I also want to avoid writing different methods doSomething(ClassA class, ...)
and doSomething(ClassD class, ...)
that are identical in all but the argument type.
If these two classes inherited directly from ClassC
, I'd have them extend a class that had all the getters and setters and properties, and pass that type into the method doSomething(..)
. Since ClassA
and ClassD
can't extend multiple classes, what's the best way to deal with this situation?
First of all, let me say that it is very good when a (by their own admission) novice developer is concerned about issues of this kind. Most developers refrain from thinking, and just write lots and lots of mindless code because this is the path that they know will work, no thinking necessary. (And having to maintain all that code never seems to be much of a problem because... it lies so far in the future!)
The approach that most Java shops follow is to avoid inheritance, because it is too complicated, and use composition instead, which also results in lots and lots of mindless code having to be written. More often than not, this is done due to a misinterpretation of the advice that said "favor composition over inheritance" as if it said "use only composition, never use inheritance".
I do not know exactly what you are working with, so I cannot advice you to drop what you are doing and use composition instead. I will just assume you have your reasons for using inheritance, so, I will not write more about composition here; if you are interested to learn more about composition, the term "composition vs. inheritance" can easily be googled.
Judging by the class structure you described, you seem to be in the typical kind of situation that would greatly benefit from the use of interfaces: In Java, a class cannot extend multiple classes, but it can implement multiple interfaces.
If two interfaces both define a method like reticulate( Spline s )
, and a class implements both of those interfaces, then it can provide a single implementation of reticulate()
, and it will satisfy both interfaces. (The disadvantage of this is that if you want your class to provide a different implementation of reticulate()
for each interface, you are stuck; it cannot be done.)
Furthermore, Java supports default
methods in interfaces, which means that a certain part of the functionality you need to offer can be coded straight into the interfaces, so that implementing classes inherit this functionality from the interface and therefore do not have to re-implement it.