Complex doesn't really extend Real. A Real number is just a special case of Complex (where the imaginary part is 0). If Complex extended Real and relied on Real's arithmetic operations then they would be incorrect. For example, an Add operation would add the real parts and return a Real.
A common mistake when learning OOP is to see inheritance relationships everywhere when in fact they're far less common. Part of the problem is that books and bad classes perpetuate this by giving you terrible example (much more in the 90s but it's still the case now). Composition is far more common.
To give you a Java example (since you don't mention a language and actual code is often more useful than pseudocode descriptions) that is probably more appropriate:
public class Complex {
private final double real;
private final double imaginary;
private Complex(double real) {
this(real, 0.0d);
}
private Complex(double real, double imaginary) {
this.real = real;
this.imaginary = imaginary;
}
public static Complex makeReal(double real) {
return new Complex(real);
}
public static Complex makeComplex(double real, double imaginary) {
return new Complex(real, imaginary);
}
public Complex add(Complex other) {
return makeComplex(real + other.real, imaginary + other.imaginary);
}
...
}
Note: this demonstrates two more useful concepts to learn:
- Immutability. You can't modify a Complex once created. This is incredibly useful;
- Factory methods. Note the constructors are private. This is deliberate and allows you to control how the objects are instantiated. For example if someone calls makeReal(3.0d) twice, you could make it return the same instance rather than creating a new one each time.