Problems with polymorphism in java

-1

This problem arose from the question next I have the following polymorphism code in java and the problem I have is that children classes are not executed

Parent Class "Player"

    public class reproductor implements Runnable {
        protected static sboolean pausado = false;
            protected static String temaactual ;
            protected static Integer posiciondepausa;
            protected static boolean ordendereproducir =true;
            volatile boolean muerto = false;
            volatile boolean reproduciendo = false;

            public void reproducir(){
                if (!reproduciendo ){

                String proximotema = com.proximotema();
                empezar(proximotema);
                }
            }
    reproductor getInstance() {
            System.out.println("bfhsoftware.sonidoambiental.reproductor.getInstance()");
            if (main.isandroid()) {
                return new reproducirAndroid();
            } else {
                //System.out.println("bfhsoftware.sonidoambiental.reproductor.getInstance()");
                return new ReproducirJava();
            }
        }
            public void empezar (String proximotema){
                //reproduciendo = true;
            }
            public void parar(){
                reproduciendo = false;
            }
            public void pausa(){
                reproduciendo = false;
                pausado = true;
            }
         @Override
            public void run (){

                if (verificador ==null) {

                    verificador = new Timer();
                }
                verificador.scheduleAtFixedRate(controlado, 0, 1000);
                //el error esta a continuacion, un bucle que vuelve loca a la memoria

                while (!muerto && ordendereproducir) {
                    reproducir();
                     //System.out.println( "reproducir");
                }
            }
        }

Child class "playAndroid"

 class reproducirAndroid extends reproductor {
      public reproducirAndroid() {
        /* Llamamos al constructor padre */
        super();
      }

      @Override
      public void empezar() {
        /* Llamamos a la implementación común de comienzo */
        super.empezar();
        /* Implementamos aquí el método en java se */
      }

      @Override
      public void parar() {
        /* Llamamos a la implementación común de parada */
        super.parar();
        /* Implementamos aquí el método en java se */
      }

      @Override
      public void pausa() {
        /* Llamamos a la implementación común de pausa */
        super.pausa();
        /* Implementamos aquí el método en java se */
      }

      /* No es necesario implementar getNumCancion ni getPosicion */
    }

Child class "reproducejava"

 class ReproducirJava extends reproductor {
  public reproducirJava() {
    /* Llamamos al constructor padre */
    super();
  }

  @Override
  public void empezar() {
    /* Llamamos a la implementación común de comienzo */
    super.empezar();
    /* Implementamos aquí el método en java se */
  }

  @Override
  public void parar() {
    /* Llamamos a la implementación común de parada */
    super.parar();
    /* Implementamos aquí el método en java se */
  }

  @Override
  public void pausa() {
    /* Llamamos a la implementación común de pausa */
    super.pausa();
    /* Implementamos aquí el método en java se */
  }

  /* No es necesario implementar getNumCancion ni getPosicion */
}

As you can see it has three classes, the reproductive class, is the father, and reprodutorjava and reproirandroid are the daughters, The problem I have is that children classes are not executed

The idea is to run a child depending on the type of operating system I am using, but I can not make it work, no child class is executed, where is the problem? I have placed Getinstance can be seen in the code of the parent class. P.S: Maybe it works, to call the parent class I do it as if it were a thread. as follows

reproductor sistemaprincipal = new reproductor();
Thread t = new Thread(sistemaprincipal);
t.start();
    
asked by Bernardo Harreguy 02.02.2018 в 18:49
source

1 answer

1

You have two possible solutions:

With static method

static reproductor getInstance() {
  System.out.println( "bfhsoftware.sonidoambiental.reproductor.getInstance()" );
  if (main.isandroid()) {
    return new reproducirAndroid();
  } else {
    //System.out.println( "bfhsoftware.sonidoambiental.reproductor.getInstance()");
    return new ReproducirJava();
  }
}

And it would be called:

Runnable sistemaprincipal = reproductor.getInstance();
Thread t = new Thread(sistemaprincipal);
t.start();

With parent instance

Instanciar the father is unnecessary as you can see in the previous section, but if you need it for something:

reproductor sistemaprincipal = new reproductor();
Thread t = new Thread(sistemaprincipal.getInstance());
t.start();
    
answered by 02.02.2018 / 21:10
source