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

232
Visualizações
Is it OK to inherit from an empty class?

I have an entity which has info1 or info2 dependently from its type, but only one of these infos should be filled. These infos have absolutely different properties. So my entity looks like this:

public class SomeEntity {

  private SomeEntityType type;

  private Info1 info1;  

  private Info2 info2;  

}

So my question is this Ok to create some empty abstract class Info and inherit Info1 and Info2 from it? (Since I think that the number of such Info classes can grow and I will need add all such infos to SomeEntity)


public class Info1 extends Info {
  /* some properties */ 
}

public class Info2 extends Info {
  /* some properties */ 
}

public class SomeEntity {

  private SomeEntityType type;

  private Info info;   

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

0

In a word - yes, it is. However, it may be more idiomatic to use an empty interface and have Info1 and Info2 implement it rather than extend an empty class.

over 4 years ago · Santiago Trujillo Relatório

0

Yes of course. That's why there is abstract class. You can create abstract class Info and write some abstract methods. You're going to implement those methods in classes Info1 and Info2.

Example

public abstract class Info {
    abstract void getInfo();
}

public class Info1 extends Info {

    void getInfo() {
        System.out.println("This is Info 1");

    }

}

public class Info2 extends Info {

    void getInfo() {
        System.out.println("This is Info 2");

    }

}

As you can see, I implemented methods differently in Info1 and Info2.

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