Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best Practices for Class Design (subclass + overriding) [closed]

Tags:

java

class

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?

like image 349
Bood Carley Avatar asked Dec 13 '22 00:12

Bood Carley


2 Answers

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.

like image 84
Marko Topolnik Avatar answered Feb 01 '23 23:02

Marko Topolnik


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.

like image 22
Tomasz Nurkiewicz Avatar answered Feb 01 '23 23:02

Tomasz Nurkiewicz