Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

190
Visualizações
Select appropriate enum class based on class member

I have an absract class that describes a general functionality of children classes. When I initialize a child class I want to set a specific enum class as a member on the parent abstract class. How can I do that?

Example: AbstractFunctionality.java

public abstract class AbstractFunctionality {
    protected String Name;
    protected String Surname;
    // specific enum class

    public AbstractFunctionality(String Name, String Surname){
         this.Name = Name;
         this.Surname = Surname;
    }
}

Child1.java

public class Child1 extends AbstractFunctionality {
    public Child1(){
        super("Jane","Austen");
    }
}

How can I specify that I want the public enum Writers in my Child1 class?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

The simpler approach is just add the enum type as the type of the field parameter of the abstract class:

public abstract class AbstractFunctionality {
    protected String Name;
    protected String Surname;
    Writers writers;

    public AbstractFunctionality(String Name, String Surname, Writers writers){
        this.Name = Name;
        this.Surname = Surname;
        this.writers = writers;
    }
}

the subclass:

public class Child1 extends AbstractFunctionality {
    public Child1(){
        super("Jane","Austen", Writers.SOME_FIELD);
    }
}

Alternatively, you can make your enum Writers implement a more general interface let us say IWriters

public abstract class AbstractFunctionality {
    protected String Name;
    protected String Surname;
    protected IWriters writers;

    public AbstractFunctionality(String Name, String Surname, IWriters writers){
         this.Name = Name;
         this.Surname = Surname;
         this.writers = writers;
    }
}

The interface:

public interface IWriters {
     ... 
}

the enum:

public enum Writers implements IWriters{
    ...
}

The benefit of this approach is that you can have different enums types implementing the same interface, and therefore they can also be used on the abstract class.

over 4 years ago · Santiago Trujillo Relatório

0

Maybe you can set generic in abstract class

public abstract class AbstractFunctionality<T extends Enum<T>> {
    protected String Name;
    protected String Surname;
    T specificEnum

    public AbstractFunctionality(String Name, String Surname,T specificEnum){
         this.Name = Name;
         this.Surname = Surname;
         this.specificEnum=specificEnum;
    }

In the sub class

public class Child1 extends AbstractFunctionality<Writers> {
    public Child1(){
        super("Jane","Austen",Writers.POEM);
    }
}
}
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda