Another problem in Java inheritance?

I have a Parent.java class and 4 child classes like Child1.java , Child2.java , etc.

There are two methods.

  • m1 ()
  • m2 ()

and one field

  • f1

Field f1 has different values ​​based on a child class.

The m1 method has a common implementation, so I put it in the Parent.java class. It also refers to the m2 method.

The m2 method has a common implementation, but this is the process field f1 , which is different for all child classes.

So my questions are:

Q1. Should I put field f1 in the parent class and allow all child classes to inherit them and initialize them in my own constructors or should I create field f1 for all child classes.

Q2. Since the m2 method has a common implementation, but the process field is f1, which does not have the same value for each child class, so I have to put it in the parent class or child class.

Q3. If I have to put the m2 method in the parent class, there is one problem: the m1 method (which has a common implementation) refers to the m2 method, so that will create any problem?

+4
source share
5 answers

Put m2 and f1 in the parent class if the implementation of m2 is the same for all classes. If there is a specific part for each child class that can be run after the common part, separate it and place it in the child classes when super.m2 () is called. Set f1 in each constructor of the child class.

The result will look something like this:

public abstract class parent { private int field = 0; public parent(int f) { field = f; } public void m1() { /* m1 implementation */ } public void m2() { /* m2 common implementation */ } } public class child1 { public child1() { super(1); } @Override public void m2() { super.m2() /* m2 child1 implementation */ } } public class child2 { public child2() { super(2); } @Override public void m2() { super.m2() /* m2 child2 implementation */ } } 

This should allow you to push the maximum amount of code back in the hierarchy as it can go. Less code duplication.

Edited to fix an attempt to access a private member from a child class. Changed to install it using the constructor.

+3
source

To my mind:

  • f1 must be in the parent class and initialized in the child constructor. Thus, the get and set methods are written only once.
  • m2 must be in the parent class.
  • m1 must also be in the parent class. Remember that you can also create abstract methods if they do not have a common implementation, but exist in all child classes. This will allow you to call it from other methods in the parent class, even though it is not defined. Also keep in mind that the parent class must also be abstract in this case.
+3
source

From what you described, I don't see any problems with this implementation:

 public class Parent { private int f1; public Parent(int f1) { this.f1 = f1; } public void m1() { } public void m2() { // do something with f1 System.out.println(f1); } } public class Child1 extends Parent { private final int DEFAULT_FIELD_VALUE = 1; public Child1() { super(DEFAULT_FIELD_VALUE); } } public class Child2 extends Parent { public Child2(int value) { super(value); } } {...} 
+2
source

What are you trying to do, for example, a template template template: http://en.wikipedia.org/wiki/Template_method_pattern

I suggest putting f1 in the parent class and declaring m2 abstract in the parent class (which the abstract class itself will do).

+1
source

If the child classes differ only in the value of f1 , then it makes no sense to even create subclasses, you just need to pass f1 in the constructor or use the static factory methods to create instances for different cases. For instance:

 public class Parent { private Value f1; private Parent(Value f1) { this.f1 = f1; } public static Parent makeChild1() { return new Parent(valueOfF1ForChild1); } public static Parent makeChild2() { return new Parent(valueOfF1ForChild2); } } 

In addition, you can check if the listing is suitable for your case.

0
source

Source: https://habr.com/ru/post/1300436/


All Articles