0

After doing a lot of reading and getting my head around inheritance, I came across a number of articles that say inheritance is pretty lackluster and interfaces are much better and I'm trying to get my head around it using some examples,

Here I have a scenario in a test automation framework which is structured as follows:

public abstract class BaseIntegrationTest { }

public abstract class BaseEducationIntegrationTest extends BaseIntegrationTest { }

public class EducationTeacherTest extends BaseEducationIntegrationTest { }

public class EducationStudentTest extends BaseEducationIntegrationTest { }

public class EducationTeacherSuite extends EducationTeacherTest { }

Is this acceptable? Should I be using interfaces in this scenario? I am struggling to understand A) Why (if so) and B) When to do so, tho I guess if I figure A) out it will go a long way towards B).

Thank you for your time.

hnefatl
  • 5,860
  • 2
  • 27
  • 49
symon
  • 670
  • 1
  • 7
  • 20
  • 2
    Welcome to Stack Overflow! Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. This unfortunately makes this question off-topic for this site. If you can [edit] your question to fit the rules of the [help], then please do so. – Joe C Oct 22 '17 at 08:54
  • Hard to say without seeing what exactly those classes do. Generally interace is preferred because you can implement multiple interfaces, and delegate their method to some other class via composition -- mimicking multiple inheritance. – Coderino Javarino Oct 22 '17 at 08:56

1 Answers1

1

Here are 2 questions that you can ask yourself when in this kind of situations:

  • Does all tests have some common behaviour and/or state? If yes, make BaseIntegrationTest an abstract class and add the common states (fields) and behaviours (methods) to it.
  • Does the specific tests need to inherit from anything else? If yes, make BaseIntegrationTest an interface since multiple inheritance is not allowed.
Sweeper
  • 213,210
  • 22
  • 193
  • 313