java - How to give a child class a modified inherited method? -
i'm facing trouble finding best oop design solution following problem: have parent abstract class classifier
includes abstract method classify(instances dataset)
. 2 classes extends classifier
, namely normalclassifier
, thresholdclassifier
. both children classes implements classify(instances dataset)
method. however, thresholdclassifier
requires classify(instances dataset)
method of form classify(instances dataset, double threshold)
, can't never work classify(instances dataset)
.
note thresholdclassifier
can't accept threshold
parameter in constructor , must have parameters in classify
method.
in case child class thresholdclassifier
share same characteristic parent requires different method signature. i'm not able extend parent class because require me implement original method signature nor makes sense not extend parent class because it's classifier
. how 1 solve such design problem in java oop? there technical term issue?
edit 1:
what did solve issue created interface called thresholdbasedclassifier
contains method setthreshold(double threshold)
. made thresholdclassifier
implement method , created internal field called threshold
. however, find ugly design solution many reasons, user forget needs set change or set threshold before calling classify(instances dataset)
edit 2:
also requirements says there can't default value threshold
.
edit 3:
my example above example common design problem i'm facing in general. i'm looking general solution not specific solution.
i can see multiple solutions:
make
thresholdclassifier
extendsnormalclassifier
.change
classifier
concrete class (remove abstract).implement
classify(instances dataset)
onthresholdclassifier
callingclassify(instances dataset, double threshold)
defaultthreshold
value.
for example:
void classify(instances dataset) { classify(dataset, 10); }
- make 2 empty methods on
classifier
, each 1 overrides apropriate one:
code:
public abstract class classifier { void classify(instances dataset) { } void classify(instances dataset, double threshold) { } } public class normalclassifier extends classifier { void classify(instances dataset) { // code here } } public class thresholdclassifier extends classifier { void classify(instances dataset, double threshold) { // code here } }
you can throw exception inside classifier methods. enforce implementation on child class.
Comments
Post a Comment