I have a class that extends a base class. The base class is more generic, and thus needs more parameters. The derived class is a specific type of base class, and thus only needs one of the two parameters the base class needs in its constructor (the derived class can provide the base class with the second parameter, but needs to do some processing first).
Is it possible to have a constructor in the derived class that then invokes the base classes constructor?
I'm aware I could just use : base(int a, int b)
if the parameters were passed in directly, but I don't think I can do this since I need to process the second variable before calling the base class constructor.
class Foo {
private int c;
public Foo(int a, int b) {
c = a + b;
}
}
class Bar : Foo {
public Bar(int a, bool plusOrMinus) {
if (plusOrMinus) {
Foo(a, 5); // calling base class constructor- AFTER processing
} else {
Foo(a, -5); // calling base class constructor- AFTER processing
}
}
}