3

I have an abstract class which has a method used by all classes that extend the class. That method is identical for each class so I don't want to have to write it over and over in those classes. The problem is that the method uses 2 variables that are declared in each class. I can't have the method in the abstract class without having those variables int eh abstract class. But if I do that, they take on the value specified in the abstract class, not the classes that extend it. How can I fix this?

Example code:

public abstract class Example {
   public String property1 = ""
   public String property2 = ""
    public ArrayList<String> getPropertyies() {
        ArrayList<String> propertyList = new ArrayList<>();
        propertyList.add(property1);
        propertyList.add(property2);
        return property1;
    }
}

public class ExampleExtension extends Example {
    public String property1 = "this is the property";
    public String property2 = "this is the second property";
}
3
  • 1
    What is a Java "abstract field"? Commented Aug 25, 2015 at 20:46
  • 2
    That is a very bad coding pattern. Any good IDE will tell you that ExampleExtension.property1 is hiding Example.property1. Keep it up if you hate people who read your code, incl. yourself.
    – Andreas
    Commented Aug 25, 2015 at 21:04
  • 3
    That same comment can be said nicely. Please comment with some respect and integrity.
    – shaydawg
    Commented Aug 27, 2015 at 8:10

4 Answers 4

12

You should limit the scope of the fields to private in the abstract class and declare a constructor for populating the values:

public abstract class Example {
    private final String property1;
    private final String property2;

    protected Example(String property1, String property2) {
        this.property1 = property1;
        this.property2 = property2;
    }
    //...
}

Subclasses would then initialize the field values in their constructors by calling the super constructor:

public class ExampleExtension extends Example {

    public ExampleExtension() {
        super("value1", "value2");
        // initialize private fields of ExampleExtension, if any
    }
    // ...
}
3
  • Why should we initialize property1 and property2 of Example (super("value1", "value2");)? Does ExampleExtension have these fields and what will be their values?
    – CoolMind
    Commented Nov 14, 2020 at 20:29
  • ExampleExtension "has" the fields because it extends Example. But it doesn't have direct read/write access because of the private visibility. If you need to read the values of the fields from the extending class, you can add a getter for them in the super class. Commented Nov 15, 2020 at 8:56
  • Thanks! Yes, these private fields must be initialized in constructor. Because they are not defined again in ExampleExtension, they exist in Example and can be accessed with getter.
    – CoolMind
    Commented Nov 17, 2020 at 21:13
2

You don't have to override the variables. You can set the initial values of your properties in the constructor:

public class ExampleExtension extends Example {

    public ExampleExtension() {
        property1 = "this is the property";
        property2 = "this is the second property";
    }

}

A better way is to use a constructor with parameters though, as Mick Mnemonic suggest in the other answer.

0
1

IMO Mick's solution is the most pragmatic, although note that you also have the option to make the properties abstract, and then use subclass polymorphicism to require that subclasses override the property implementation:

public abstract class Example {
   public abstract String getProperty1();
   public abstract String getProperty2();

    public ArrayList<String> getPropertyies() {
        ArrayList<String> propertyList = new ArrayList<>();
        propertyList.add(getProperty1());
        propertyList.add(getProperty2());
        return propertyList;
    }
}

public class ExampleExtension extends Example {
    public String getProperty1() { return "this is the property"};
    public String getProperty2() { return "this is the second property"};
}
1
  • When we write abstract String, it will show error: "Modifier 'abstract' not allowed here".
    – CoolMind
    Commented Nov 14, 2020 at 20:33
0

Make what differs(e.g. property1,property2) abstract method in this case . Search for related reading on template pattern

public abstract class Example {
       public ArrayList<String> getPropertyies() {
        ArrayList<String> propertyList = new ArrayList<>();
        propertyList.add(getProperty1());
        propertyList.add(getProperty2());
        return property1;
    }

    public abstract getProperty1();//overriden by other class that has diff value for property1
    public abstract getProperty2();//overriden by other class that has diff value for property2
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.