I have defined two classes below:
public class junk {
private BigInteger a = BigIngeger.valueOf(1), b=BigInteger.valueOf(2), c;
public void DoSomething() {
c = a.add(b);
}
}
public class junkChild extends junk {
private BigDecimal a = BigDecimal.valueOf(1), b=BigDecimal.valueOf(2), c;
}
public class myClass {
public static void main(String args[]) {
junk a1 = new junkChild();
a1.DoSomething();
}
}
Unfortunately this does not work.
I just want to change a, b, con BigDecimalin junkChildwithout overwriting DoSomething()again. Even if I have to write it again, the code will be exactly the same, so there must be a way I can do this work without writing it. The function DoSomethingshould check that the type of a1a method of addthe correct type of input and return, and if so, call him, without worrying about what type a, b, care located.
Can this be done?
source
share