views:

139

answers:

4

suppose for example a program is to process complex numbers consisting of real and imaginary parts. in a complex number, the real and imaginary parts behave like real numbers so all of the operations (+,-,/,*,sqrt,sin,cos, etc) can be inherited form the class of objects called REAL instead of having to be written in code.

Does the assertion that the child class Complex need not write any code seem plausible?

Does this make sense in terms of the data members each class must maintain? Why or why not?

Does it make sense in terms of methods? Is there a better approach for creating a class Complex using an existing class Real?

+6  A: 

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:

  1. Immutability. You can't modify a Complex once created. This is incredibly useful;
  2. 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.
cletus
+1 for advocating composition instead of inheritance. If we consider the Java primitive `double` (or the boxed equivalent `Double`) to be a `Real` class, then your `Complex` class actually is the *composition* of two `Real` instances. `Complex` doesn't have to *inherit* from `Double` at all.
Daniel Pryden
A: 

I don't really and fully understand your question. Complex is a datatype which you implement by using one rational approach. A complex is made with two real parts, the first as is, the second assumed to be the prefactor of the imaginary unit. You could implement the complex number internally with strings, bit arrays or integers, one per each digit, but that would be very stupid.

The Complex class does need code. a complex is not a real, it's a complex, and behaves in a different way with respect to a real. You cannot even create a complex as a child class of real, they are two different things.

Stefano Borini
A: 

A complex number is a combination of two real numbers, x + i * y, where i is sqrt(-1).

Your Complex class would need to handle the extra logic needed around how to add/subtract/multiply/divide/etc two complex numbers together.

Eg. (a + i b) * (c + i d) = (ac − bd) + i (bc + ad).

This is the code that would go in your Complex class.

It would work the other way though - if you already had a Complex class and no Real class (however unlikely that is), you could define a Real class as a subclass of Complex, and just lock the imaginary component to 0.

So Complex inheriting from Real wouldn't work. A better approach would involve not inheritence, but composition - a Complex number is composed of two real numbers.

Blorgbeard
thank you!! tat makes sense :)
shalini
A: 

I do not understand your question at all. In the domain of mathematics, real and complex numbers are contained one in the other. Real numbers are complex numbers with their complex part set to 0; in this sense, the complex set is a superset of real numbers; they are numbers, but none of them share characteristics to support the assumption of one being an specialization/generalization of the other. Now, in the datatype world of common languages like C and Java, you'll never find a complex type given for primitive type (i.e. int, char, etc). Then, if you want to extend the datatype set to add complex numbers support, you'll have to create a class Complex with the operations defined by mathematicians for the complex numbers.

JPCF