I want to design class hierarchy. Super class has an abstract method, sub class has to override this method, but my problem is: this method use data of super class (instance variable).
I've designed some style to solve this problem:
Style 1: Use private instance variable and create other instance variable in sub class
class SuperClass {
private T data;
public SuperClass(T data) { /* ... */}
public abstract void doSomething();
}
class SubClass extends SuperClass {
private T data; // use another variable
public SubClass(T data) {
super(data);
this.data = data;
}
public void doSomething() {
// use 'data'
int length = data.getLength(); // for example...
}
}
Style 2: Use protected instance variable:
class SuperClass {
protected T data;
public SuperClass(T data) { /* ... */}
public abstract void doSomething();
}
class SubClass extends SuperClass {
public void doSomething() {
// use 'data'
int length = data.getLength(); // for example...
}
}
Style 3: Use private instance variable and protected method:
class SuperClass {
private T data;
public SuperClass(T data) { /* ... */}
public abstract void doSomething();
protected T getData() {
return data;
}
}
class SubClass extends SuperClass {
public void doSomething() {
// use 'data'
int length = getData.getLength(); // for example...
}
}
Which of them is better? Or have more solutions to solve my problem better?
The third approach is usually preferred and, if you design for public subclassing, is a must. A superclass wants to maintain as much control as possible over its internals.
If, on the other hand, you are developing a closed hierarchy that is all in the same package, or at least all in the same project, you can choose to expose the var as protected, if there's any benefit to be had.
Duplicating a private var in the subclass is a failure on all counts, however. I can't think of any scenario where that would help anything.
IMHO solution #1 is poor because it duplicates the reference unnecessarily, leading to confusion (it's not even about memory consumption or consistency)
Solution #2 and #3 are equally good. If you make your data
field final
:
protected final T data;
solution #2 is best you can get.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With