Java – Different ways to implement methods

Different ways to implement methods… here is a solution to the problem.

Different ways to implement methods

I’m starting to code and I found this issue about implementing a method from another project.

For example, in this code, I can use the methods I

used in CreateECASPageObject in my project CreateECAsDefinitions, but I know that if I use extensions in this class, I can use those methods as well. I’m confused.

I can use this method:
IngresarECAs.FirstMethod();

Do they use extends the same way I use them?

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

wonder what the difference is if I type :

private CreateECASPageObject IngresarECAs;

Or put it this way:

public class CreateECAsDefinitions extends CreateECASPageObject

In the beginning.

Solution

This is not a difference in implementation methods. This is the difference between composition and inheritance

If you do not need or want to CreateECAsDefinitions or inherit all the accessible fields and methods of CreateECASPageObject, then you should not use extensions. In other words, you hide the functionality of possible parent classes by “combining” instead of “inheriting”.

Given that you don’t show method definitions, it’s hard to say which way should be preferred in your scene, but if you find yourself writing this pattern for every method in the CreateECAsPageObject class, you might want to use inheritance.

private CreateECAsPageObject ingresarECAs ;

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

Related Problems and Solutions