user11187837
user11187837

Reputation:

Difference ways of implementing methods

I'm starting coding and I find this question about implementing methods from another project.

For example in this code I can use the methods I use in CreateECASPageObject in my project CreateECAsDefinitions, but I know if I use extends in this class , I also can use the methods. I am confused.

I can use the method like this: IngresarECAs.FirstMethod() in the project below;

Are they the same using extends and the way I am using?

public class CreateECAsDefinitions {

    private LectorExcel excel;
    private static  XSSFSheet hoja;
    private int fila;

      @Before
        public void before(Scenario scenario) {
            this.scenarioActual = scenario;
        }


      @Steps  
      private CreateECAsPageObject IngresarECAs ;
      private ValidacionCasosPageObject validacionCasos;

I want to know what the difference if I put:

private CreateECASPageObject IngresarECAs;

or put this:

public class CreateECAsDefinitions extends CreateECASPageObject

At the beginning.

Upvotes: 3

Views: 80

Answers (2)

OneCricketeer
OneCricketeer

Reputation: 191711

This isn't a difference of implementing methods. This is the difference between composition and inheritance

If you do not need or want CreateECAsDefinitions or inherit all accessible fields and methods of CreateECASPageObject, then you should not use extends. In other words, you are hiding away functionality of the possible parent class by "composing" rather than "inheriting".

Given that you have not shown the method definition, it is hard to say what way should be preferred given your scenario, but if find yourself writing this pattern for each and every method within CreateECAsPageObject class, then you might want to then use inheritance.

private CreateECAsPageObject ingresarECAs ;

public void firstMethod() {
    ingresarECAs.firstMethod();
}

Upvotes: 3

davidxxx
davidxxx

Reputation: 131346

You ask the difference between inheritance and composition. It is a broad subject.
Inheritance (extends keyword) and composition (private instance field in the class) are two distinct approaches to set a dependency between two classes.
The general idiom is favor composition over inheritance because composition is more flexible in terms of possibility (for example you can compose with multiple fields but inheriting a single class) and reduces the coupling between the two classes. But in some cases, inheritance may make sense to reduce boiler plate code (repetitive code) or verbosity.

In your case with inheritance you write firstMethod() and with composition you have to write ingresarECAs.firstMethod(). In such a simple case, hard to spot a great design advantage in the first or the second way.
But defining a base test class may make sense in some case to force other test classes to rely on the same logic.
So definitively I think that you should probably think about your requirements and set the cursor between inheritance and composition at the right place according to them.

Upvotes: 0

Related Questions