0

I have a design problem in Java. I have

  • an abstract class Feature which represents features (machine learning), and
  • an abstract class Normalizable which inherits from Feature and represents features which can be normalized.

A concrete feature can be an instance of either Feature or Normalizable.

I would like to introduce an abstract class called Probablity which inherits from Feature and represents features on which some sort of probability calculations can be done.

An actual feature should either be an instance of Feature (not normalizable and no probability) or it should be an instance of Normalizable, or Probability or both.

The problem now is that an actual feature cannot inherit from both Normalizable and Probability.

How can I solve this issue? I think using interfaces is not the solution because all of the mentioned classes contains implementations.

4
  • Using interfaces is the only solution that come to my mind if you can't design a class structure. Otherwise you may go to a design pattern like strategy, Where the type of the algorithm/implementation used has to be determined at runtime. Commented Mar 7, 2016 at 17:39
  • 1
    It helps to name your classes to clarify your intent. For instance, it makes little sense to call a subclass of Feature a Probability. It's better to call it say ProbabilisticFeature. To further help you, can you identify some methods with appropriate names on each of these classes? Commented Mar 7, 2016 at 18:00
  • @ImeshaSudasingha But interfaces cannot have actual implementations. So I have to push the implementations in my actual classes (implementing the interfaces) which creates lot of duplicate code. Commented Mar 7, 2016 at 18:52
  • So you have classes (abbreviated) F,N,P with $P\to F\gets N$ (and possibly more, all inheriting from F), do I get this right? And then yo want to have instances of F with any combination of abilities from those? stackoverflow.com/a/2516100/4142984 might help. Commented Mar 7, 2016 at 20:12

1 Answer 1

1

You can create a class NormalizablePrbability.java Use Composition and have Probability.java and Normalize.java as fields in newly created class NormalizablePrbability.java. You can extend NormalizablePrbability.java from Feature.java and override the methods and in each method you can provide the combined features of both Probability.java and Normalize.java.

Now you can have your concrete classes which can extends from Feature.java or Normalize.java or Probability.java or NormalizablePrbability.java

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your answer. Your assumption is not correct (and my explanation was not so good). Probability.java inherits from Feature.java but not from Normalizable.java. An object can be a subtype of Normalizable, Probability or both. The problem is when the class should be a subtype of both.
@BlackHawk I am sorry for understanding your question wrongly, I have made the corrections in my answer, hope it helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.