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

242
Visualizações
Does inheritance can affect performances of an application?

I wonder if performances of an application could be improved by using the more superclasses you can. My question is about Kotlin, but I assume the answer will be the same for Java.


Let's say you have this inheritance schema (the right class is a subclass of the class on his left):
A < B < C < D < E < F < G < ... And so on until Z.

Saying you don't need all the stuff defined in all the subclasses, but only the attributes and the functions of the A class. For an obscure reason, your code uses only Z class.

My question is quite simple: If you change your code to only use A class instead of Z class, will the code be more performant?

Thank you in advance for your answers.

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

0

If

"change your code to only use A class instead of Z class"

includes construction, then there is a trivial answer:

Creating and storing an object with less attributes/fields is cheaper than creating an object with more attributes, both in terms of memory and in terms of time it takes to initialize.

Other than that, I would not expect a significant effect on performance.


That said, I wouldn't worry about performance here - choose the type that provides the best abstraction for what you want to do with this. If your code doesn't care what specific subclass you are using and doesn't need any of the functionality provided by the subclasses, use the more general type. That way your code remains more flexible. If your code however needs such functionality at some point, i.e. you have to introduce casts further down the line, then you have gone too far.

over 4 years ago · Santiago Trujillo Relatório

0

The answer is yes. For example, if you invoke the method z.someMethod(). The Hotspot will look up the method in Z class first. If HotSpot does not found the target method, it will look up the method in Z's parent. And continue these steps until it found the target method. So it will take more time to lookup the method if the inheritance chain is long.

However, HotSpot using some cache to accelerate the process.

HotSpot VirtualCalls

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