Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference ways of implementing methods

Tags:

java

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.


1 Answers

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();
}
like image 153
OneCricketeer Avatar answered Apr 20 '26 09:04

OneCricketeer