Similar Sub-Classes
- by praks5432
Lets say I have a class A that is fairly simple like this -
public class A{
private int randomField = 0;
protected int key;
protected double dmg;
}
Now I want to write a number of sub-classes that inherit the protected fields and only differ based on the initial values that are assigned to those fields - for example, if I wrote two subclasses B and C, the only difference between those two sub-classes would be that the values key and dmg would have different values. They would share a method, set, which would be exactly the same, in that it would affect the same variable.
I find when I'm writing these sub-classes I'm repeating myself, as I just change the constructor to set different initial values to key and dmg, and simply copy and paste the set method.
Is there a 'good' way to do this?