3

I'm coming up with a set of Java classes for templating.

I have an abstract template:

package Test;

public class Abstract {
    protected String template = "ABSTRACT TEMPLATE";
    public Abstract() {

    }

    public void Render() {
        System.out.println("FROM ABSTRACT RENDER:");
        System.out.println(this.template);
    }
}

And an actual one:

package Test;

public class Actual extends Abstract {
    protected String template = "ACTUAL TEMPLATE";
    public Actual() {
        super();
        System.out.println("FROM ACTUAL CONSTRUCTOR:");
        System.out.println(this.template);
    }

    public void Test() {
        System.out.println("FROM ACTUAL TEST:");
        System.out.println(this.template);
    }
}

I'm having trouble getting the extending class to reset the value of a protected attribute (in this case template) and letting be used by the abstract method.

This is my use case:

Actual actual = new Actual();
actual.Render();
actual.Test();

And this is my output:

FROM ACTUAL CONSTRUCTOR:
ACTUAL TEMPLATE
FROM ABSTRACT RENDER:
ABSTRACT TEMPLATE <--- this is the problem, why not "ACTUAL"?
FROM ACTUAL TEST:
ACTUAL TEMPLATE

How do I override that value from the child class? If I don't set it to anything, calling the abstract method will always say the attribute is null, even though it's set in the child class.

Robin Green
  • 32,079
  • 16
  • 104
  • 187
Alexander Kleinhans
  • 5,950
  • 10
  • 55
  • 111

0 Answers0