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:

  1. make thresholdclassifier extends normalclassifier.

  2. change classifier concrete class (remove abstract).

  3. implement classify(instances dataset) on thresholdclassifier calling classify(instances dataset, double threshold) default threshold value.

for example:

void classify(instances dataset) {     classify(dataset, 10); } 
  1. 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

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -