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

279
Visualizações
two Java classes that share many of their methods but dont have a "is a" relationship

i am implementing two classes that share many methods but i cant make one inherit from the other as it doesn't make sense so i opted for a shared interface with all the shared methods. but i realized that many methods have the same implementation for both classes too so my question would be how can i make these methods "inherit" from a default method kind of like "super" methods in inheritance?

one solution that im thinking of is creating a parent class so that both my classes can inherit from it

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

0

Inheritance should be used when you need to refer to a common ancestor, not to 'share' common code. You say that the two objects do not have this relationship but do have common code - the obvious approach is to factor that out into it's own class (or interface or whatever) and compose into your objects:

You didn't provide an example so I'll make one up:

class CommonStuff {
    // common members here
}

class One {
    private final CommonStuff common;
}

class Two {
    private final CommonStuff common;
}

No need for inheritance and its inherent hard coupling (pun intended).

over 4 years ago · Santiago Trujillo Relatório

0

Yes Abstract Class is the right approach for this if you have some default implementation for methods otherwise you can go for interface.

Write all your common piece of logic in your Abstract class methods and make these methods abstract if you want the subclass to override the implementation otherwise you don't need to make them abstract methods

over 4 years ago · Santiago Trujillo Relatório

0

To expand on my comment: you want to use composition as well as the decorator pattern with delegation

For example:

public interface Foo {
    public void a();
    public void b();
    
}
// used as the inner class
public class ConcreteFoo implements Foo {
    public void a() {
        // implementation for a
    }
    
    public void b() {
        // implementation for b
    }
}
public class MyContainerA implements Foo {
    private Foo innerFoo;
    
    public MyContainerA(Foo innerFoo) {
        this.innerFoo = innerFoo;
    }
    
    public void a() {
        // delegate the method call to the contained object
        innerFoo.a();
    }
    
    public void b() {
        // delegate
        innerFoo.b();
    }
    
    public void methodOnlyInContainerA() {
        
    }
}
public class MyContainerB implements Foo {
    private Foo innerFoo;
    
    public MyContainerB(Foo innerFoo) {
        this.innerFoo = innerFoo;
    }
    
    public void a() {
        innerFoo.a();
    }
    
    public void b() {
        innerFoo.b();
    }
    
    public void methodOnlyInContainerB() {
        
    }

}    

elsewhere:

Foo foo = new ConcreteFoo();

// both guys below use the same inner class
MyContainerA contA = new MyContainerA(foo);
MyContainerB contB = new MyContainerB(foo);
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