Multiple child class vs Single child class – Java Inheritance

huangapple go评论158阅读模式
英文:

Multiple child class vs Single child class - Java Inheritance

问题

I have a question that extending a Parent class to multiple child classes is a good practice or maintaining a single child is a good practice? Does this affect scalability? All the classes will be executed at the same time. So I have this question.

例如,我只使用了4个类。在实际情况下,将有83个类使用相同的方法并同时执行。

public interface A
{
  methodA();
}

public class B implements A
{
  methodA()
  {
    .....
  }
}

public class C
{
  B b = new B();
  b.methodA();
}

public class D
{
  B b = new B();
  b.methodA();
}

或者

public interface A
{
  methodA();
}

public class B implements A
{
  methodA()
  {
    .....
  }
}

public class C extends B
{
  super.methodA();
}

public class D extends B
{
  super.methodA();
}

或者

public interface A
{
  methodA();
}

public class B implements A
{
  methodA()
  {
    .....
  }
}

public class C extends B
{
  super.methodA();
}

public class D extends C
{
  super.methodA();
}
英文:

I have a question that extending a Parent class to multiple child classes is a good practice or maintaining a single child is a good practice? Does this affects the scalability? All the classes will be executed at the same time. So I have this question.

for example I have used only 4 classes. In real time It will be like 83 classes will be using the same method and gets executed at once.

public interface A
{
  methodA();
}

public class B implements A
{
  methodA()
  {
    .....
  }
}

public class C
{
  B b = new B();
  b.methodA();
}

public class D
{
  B b = new B();
  b.methodA();
}

Or

public interface A
{
  methodA();
}

public class B implements A
{
  methodA()
  {
    .....
  }
}

public class C extends B
{
  super.methodA();
}

public class D extends B
{
  super.methodA();
}

Or

public interface A
{
  methodA();
}

public class B implements A
{
  methodA()
  {
    .....
  }
}

public class C extends B
{
  super.methodA();
}

public class D extends C
{
  super.methodA();
}

答案1

得分: 1

遵循 SOLID 原则。这些设计原则鼓励我们创建更易维护、易理解和灵活的软件。因此,随着我们的应用程序规模的增长,我们可以降低其复杂性,从而在未来避免许多麻烦!

英文:

Go through the SOLID principle. Its a design principles encourage us to create more maintainable, understandable, and flexible software. Consequently, as our applications grow in size, we can reduce their complexity and save ourselves a lot of headaches further down the road!

答案2

得分: 0

你可以使用 privatedefault 访问修饰符来定义 methodA 的代码。
课程:接口和继承(Java™ 教程 > 学习 Java 语言)

英文:

You could define the code for methodA if you use the private or default access modifier.
Lesson: Interfaces and Inheritance (The Java™ Tutorials > Learning the Java Language).

interface A {
    private String method() {
        return "stack overflow";
    }
}

class B implements A {
    String string = A.super.method();
}

huangapple
  • 本文由 发表于 2023年6月5日 02:27:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76401873.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定