Similar Sub-Classes
Posted
by
praks5432
on Stack Overflow
See other posts from Stack Overflow
or by praks5432
Published on 2012-06-17T03:05:20Z
Indexed on
2012/06/17
3:16 UTC
Read the original article
Hit count: 109
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?
© Stack Overflow or respective owner