3

I and doing an app in Android which can run different ML models and I want to create some classes to represent the data of the results, each ML model has it owns variables/result, for example, a classification problem has labels meanwhile other do not have them.

I want to use inheritance in java and doing something like Result as interface and father of ClassificationResult and RegresionResult so I could do:

Arraylist<Result> resultList;
ClassificationResult cr = new ClassificationResult...
RegresionResult rr = new RegresionResult...
resultList.add(cr)
resultList.add(rr)  

But I face the problem that I cannot call methods from the children like ClassificationResult.getLabels() (I could have all the method of all the children in the interface but it seems weird for me)

I saw some patterns, Factory and Builder but I face the previously described problem with the Factory pattern and with the Builder (having all the variables in one class) most of them would not be initialized.

I would appreciate some ideas about how I should design this

JMP
  • 75
  • 7
  • 1
    See my post which is about polymorphism I think [this](https://stackoverflow.com/questions/55861318/how-to-use-polymorphism-to-build-list-in-java) will help you – Alireza Bideli Sep 23 '19 at 09:26
  • Yeah, I totally understand your post, but for example, if you want to add more functionalities to `Circle`, for example `getPositionCentre()` that method is not in Shape so you cannot call it. @Alireza Bideli – JMP Sep 23 '19 at 11:17
  • you can cast your object to Circle and then call its methods @JMP – Alireza Bideli Sep 23 '19 at 11:20
  • 1
    @AlirezaBideli Or he can cast his object to `Duck` and call its `quack()` method :) and then catch the `ClassCastException` and if caught, tell the user that "your duck is already cooked and does not support quacking anymore" :) – Honza Zidek Sep 23 '19 at 13:25

1 Answers1

4

If it looks like a duck and quacks like a duck but it needs batteries, you probably have the wrong abstraction.

(Search internet for this phrase and read the articles you will find.)

In other words, when you do not know how to distinguish the objects, do not consider them as different specifications of a single interface.

Unless you implement a low-level framework, if you feel tempted to use instanceof, or - better said - if using the instanceof is the only way to distinguish your objects, most probably your design is wrong from the very beginning.

Rather think about what your objects really have in common. Only if you find anything, make it an interfaced method and let each of your object implement it.

If you are a zoologist, you can consider dogs and cats as just subtypes of a single interface. If you are blind and need a guide dog, then these two are completely different classes.

Honza Zidek
  • 9,204
  • 4
  • 72
  • 118
  • HI, Thanks for your reply, I think I get what you mean, I should have different classes (without inheritance) as they do not share the same behaviour, I think I will go with generic classes.@Honza Zidek – JMP Sep 23 '19 at 11:34
  • @JMP They do not have to share the *same* behaviour, but the behaviour which is different should share *something in common*. Dogs bark and cats miaow, which is very different behaviour, but both can answer the very same question "How does the animal go?". If you find a good natural name for the "same" action, it is the same interface. – Honza Zidek Sep 23 '19 at 13:29