5

I have a superclass like this which I expect alot of classes to inherit:

public abstract class Super {
    protected Object myField; //Not used anywhere in this class
    //a load more code here which does useful stuff
}

All these classes will need to use an instance of myField. However the superclass does not. Have I made a bad design decision somewhere?

pb2q
  • 58,613
  • 19
  • 146
  • 147
mogronalol
  • 2,946
  • 8
  • 38
  • 56
  • Why do all subclasses need it but its not used in the Baseclass at all? – Stefan Sep 06 '12 at 15:24
  • Protected fields are a bad choice at its root. [Here](http://stackoverflow.com/questions/3182653/are-protected-members-fields-really-that-bad) is why? – RBT Feb 04 '17 at 00:16

7 Answers7

6

Not necessarily. If all the subclasses need the same field for the same reason, then that's no different than providing any other common functionality in a base class. as your classes grow you may find that you add common functionality which uses this field (e.g. referencing it in an equals/hashCode method).

Now, if the field is of type Object and each sub-class shoves something completely different into it, i would consider that a code smell.

jtahlborn
  • 52,909
  • 5
  • 76
  • 118
  • [Protected fields are bad.](http://stackoverflow.com/questions/3182653/are-protected-members-fields-really-that-bad) – RBT Feb 04 '17 at 00:19
  • @RBT i personally rarely use protected members myself. that said, they have their place. – jtahlborn Feb 04 '17 at 00:26
5

Well IMHO, a field should not be present in a class if it's not really used by that class. What it seems to me that you really want here is to have a base class that tells its subclasses "you should ALL have some way of keeping state for X but I (the base class) will not modify that X state, in which case you should make an abstract method in order to convey that message, something like this:

public abstract class Super {
    protected abstract Object getMyField();
}
Shivan Dragon
  • 15,004
  • 9
  • 62
  • 103
3

It's hard to say with such a vague description, but it would seem like you could do some generalization and push some common code up into your superclass. If your subclasses are doing something similar with the field then some commonality could be found (using template methods or strategies to handle subclass-specific differences), otherwise if every subclass is doing something different with it then what's the point of using a common field?

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
2

No, I don't think so. Abstract class serve that purpose (Have common functionality in base class and let subclass implement only specific required functionality).

kosa
  • 65,990
  • 13
  • 130
  • 167
2

So, if you don't use that field in class Super - why do you need it there?

Perhaps your super class would provide an interface to interact with this field in generic way, for example:

public abstract class Super<T> {
    protected T myField; 

    public T getField() {
        return myField;
    }
}

public class Child extends Super<String> {
    public Child( String label ) {
        super.myField = label;
    }
}
stemm
  • 5,960
  • 2
  • 34
  • 64
1

As stated in this tuturial

A protected field or method is accessible to the class itself, its subclasses, and classes in the same package.

This means that the protected fields have been designed precisely to have these characteristics.

enzom83
  • 8,080
  • 10
  • 68
  • 114
1

Just on a lighter note The only thing common in your hirarchy is one field then you should get rid of abstract class and Create one Marker Interface.

Amit Deshpande
  • 19,001
  • 4
  • 46
  • 72