views:

172

answers:

5
+3  A: 

In my opinion:

  1. f1 should be in the parent class and initialized in the child constructor. This way, the get and set methods are only written once.
  2. m2 should be in the parent class.
  3. m1 should be also be in the parent class. Don't forget that you can also make methods abstract if they do not have a common implementation but exist in all child classes. This would allow you to call it from other methods in the parent class despite not being defined there. Also keep in mind that the parent class would also need to be abstract in this case.
R. Bemrose
+1 ........................................... :)
Yatendra Goel
+3  A: 

Place m2 and f1 in the parent class if m2's implementation is the same for all classes. If there's a specific part for each child class that can be run after the common part - separate it and place it in the child classes while calling super.m2(). Set f1 in each child class's constructor.

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's implementation */ }
    public void m2() { /* m2's common implementation */ }
}

public class child1 {
    public child1() {
        super(1);
    }

    @Override
    public void m2() { super.m2() /* m2's child1 implementation */ }
}

public class child2 {
    public child2() {
        super(2);
    }

    @Override
    public void m2() { super.m2() /* m2's child2 implementation */ }
}

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

Edited to fix trying to access a private member from a child class. Changed to setting it using the constructor.

Daniel Bingham
+1 ........................................... :)
Yatendra Goel
Would it be better to intialize fields directly in the child classes or intialize them by passing the values as parameter in super() call to the super class constructor.
Yatendra Goel
I'd go with passing the values to super.
Daniel Bingham
Kindly make the above "field" in Parent class as protected. Private field can't be inherited. So that it would not be confusing for others.
Yatendra Goel
+1  A: 

What you are trying to do sound like Template Method design pattern: 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 will make the class abstract itself).

ruibm
+1 for the wiki link
Yatendra Goel
+2  A: 

From what you described, I don't see any problem 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);
   }
}

{...}
bruno conde
+1 ................................ :)
Yatendra Goel
A: 

If the child classes differ only in the value of f1 then there is no point in even creating subclasses, you should just pass f1 in a constructor or use static factory methods to create instances for the different cases. For example:

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);
    }
}

Also, you might want to check out if an enumeration is suitable for your case.

starblue
First, In the above problem I mentioned only one field but in actual, there are 4 fields and all the four fields (of String type) have long and constant value, so upto my thinking It would be better to make child classes each containing final static values of those 4 fields and pass them as attributes in the super() call... What you think? Am I correct?Second, Could you please edit your answer to throw some light on static factory methods. I haven't used them before.
Yatendra Goel