How to implement RealNumber and ComplexNumber inheritance?
Hopefully not too academic...
Let's say I need real and complex numbers in my SW library.
Based on is-a (or here) relationship, real number is a complex number, where b in imaginary part of complex number is simply 0.
On the other hand, my implementation would be, that child extends parent, so in parent RealNumber I'd have real part and child ComplexNumber would add imaginary art.
Also there is an opinion, that inheritance is evil.
I recall like yesterday, when I was learning OOP at university, my professor said, this is not a good example of inheritance as absolute value of those two is calculated differently (but for that we have method overloading/polymorfism, right?)...
My experience is, that we often use inheritance to solve DRY, as a result we have often artificial abstract classes in hierarchy (we often have problem to find names for as they do not represent objects from a real world).
design
|
show 6 more comments
Hopefully not too academic...
Let's say I need real and complex numbers in my SW library.
Based on is-a (or here) relationship, real number is a complex number, where b in imaginary part of complex number is simply 0.
On the other hand, my implementation would be, that child extends parent, so in parent RealNumber I'd have real part and child ComplexNumber would add imaginary art.
Also there is an opinion, that inheritance is evil.
I recall like yesterday, when I was learning OOP at university, my professor said, this is not a good example of inheritance as absolute value of those two is calculated differently (but for that we have method overloading/polymorfism, right?)...
My experience is, that we often use inheritance to solve DRY, as a result we have often artificial abstract classes in hierarchy (we often have problem to find names for as they do not represent objects from a real world).
design
4
this looks like covered in prior question: Should rectangle inherit from square?
– gnat
5 hours ago
1
@gnat Oh man, that was another example I wanted to use... Thanks!
– Betlista
4 hours ago
2
... Note that the sentence "real number is a complex number" in the mathematical sense is only valid for immutable numbers, so if you use immutable objects, you can avoid the LSP violation (same holds also for squares and rectangles, see this SO answer).
– Doc Brown
3 hours ago
3
... Note further the absolute value calculation for complex numbers works also for real numbers, so I am not sure what your professor meant. If you implement an "Abs()" method correctly in an immutable complex number and derive a "real" from it, the Abs() method will still deliver correct results.
– Doc Brown
3 hours ago
2
Possible duplicate of Should rectangle inherit from square?
– BobDalgleish
1 hour ago
|
show 6 more comments
Hopefully not too academic...
Let's say I need real and complex numbers in my SW library.
Based on is-a (or here) relationship, real number is a complex number, where b in imaginary part of complex number is simply 0.
On the other hand, my implementation would be, that child extends parent, so in parent RealNumber I'd have real part and child ComplexNumber would add imaginary art.
Also there is an opinion, that inheritance is evil.
I recall like yesterday, when I was learning OOP at university, my professor said, this is not a good example of inheritance as absolute value of those two is calculated differently (but for that we have method overloading/polymorfism, right?)...
My experience is, that we often use inheritance to solve DRY, as a result we have often artificial abstract classes in hierarchy (we often have problem to find names for as they do not represent objects from a real world).
design
Hopefully not too academic...
Let's say I need real and complex numbers in my SW library.
Based on is-a (or here) relationship, real number is a complex number, where b in imaginary part of complex number is simply 0.
On the other hand, my implementation would be, that child extends parent, so in parent RealNumber I'd have real part and child ComplexNumber would add imaginary art.
Also there is an opinion, that inheritance is evil.
I recall like yesterday, when I was learning OOP at university, my professor said, this is not a good example of inheritance as absolute value of those two is calculated differently (but for that we have method overloading/polymorfism, right?)...
My experience is, that we often use inheritance to solve DRY, as a result we have often artificial abstract classes in hierarchy (we often have problem to find names for as they do not represent objects from a real world).
design
design
asked 5 hours ago
BetlistaBetlista
19925
19925
4
this looks like covered in prior question: Should rectangle inherit from square?
– gnat
5 hours ago
1
@gnat Oh man, that was another example I wanted to use... Thanks!
– Betlista
4 hours ago
2
... Note that the sentence "real number is a complex number" in the mathematical sense is only valid for immutable numbers, so if you use immutable objects, you can avoid the LSP violation (same holds also for squares and rectangles, see this SO answer).
– Doc Brown
3 hours ago
3
... Note further the absolute value calculation for complex numbers works also for real numbers, so I am not sure what your professor meant. If you implement an "Abs()" method correctly in an immutable complex number and derive a "real" from it, the Abs() method will still deliver correct results.
– Doc Brown
3 hours ago
2
Possible duplicate of Should rectangle inherit from square?
– BobDalgleish
1 hour ago
|
show 6 more comments
4
this looks like covered in prior question: Should rectangle inherit from square?
– gnat
5 hours ago
1
@gnat Oh man, that was another example I wanted to use... Thanks!
– Betlista
4 hours ago
2
... Note that the sentence "real number is a complex number" in the mathematical sense is only valid for immutable numbers, so if you use immutable objects, you can avoid the LSP violation (same holds also for squares and rectangles, see this SO answer).
– Doc Brown
3 hours ago
3
... Note further the absolute value calculation for complex numbers works also for real numbers, so I am not sure what your professor meant. If you implement an "Abs()" method correctly in an immutable complex number and derive a "real" from it, the Abs() method will still deliver correct results.
– Doc Brown
3 hours ago
2
Possible duplicate of Should rectangle inherit from square?
– BobDalgleish
1 hour ago
4
4
this looks like covered in prior question: Should rectangle inherit from square?
– gnat
5 hours ago
this looks like covered in prior question: Should rectangle inherit from square?
– gnat
5 hours ago
1
1
@gnat Oh man, that was another example I wanted to use... Thanks!
– Betlista
4 hours ago
@gnat Oh man, that was another example I wanted to use... Thanks!
– Betlista
4 hours ago
2
2
... Note that the sentence "real number is a complex number" in the mathematical sense is only valid for immutable numbers, so if you use immutable objects, you can avoid the LSP violation (same holds also for squares and rectangles, see this SO answer).
– Doc Brown
3 hours ago
... Note that the sentence "real number is a complex number" in the mathematical sense is only valid for immutable numbers, so if you use immutable objects, you can avoid the LSP violation (same holds also for squares and rectangles, see this SO answer).
– Doc Brown
3 hours ago
3
3
... Note further the absolute value calculation for complex numbers works also for real numbers, so I am not sure what your professor meant. If you implement an "Abs()" method correctly in an immutable complex number and derive a "real" from it, the Abs() method will still deliver correct results.
– Doc Brown
3 hours ago
... Note further the absolute value calculation for complex numbers works also for real numbers, so I am not sure what your professor meant. If you implement an "Abs()" method correctly in an immutable complex number and derive a "real" from it, the Abs() method will still deliver correct results.
– Doc Brown
3 hours ago
2
2
Possible duplicate of Should rectangle inherit from square?
– BobDalgleish
1 hour ago
Possible duplicate of Should rectangle inherit from square?
– BobDalgleish
1 hour ago
|
show 6 more comments
3 Answers
3
active
oldest
votes
Even if in a mathematical sense, a real number is a complex number, it is not a good idea to derive real from complex. It violates the Liskow Substitution Principle saying (among other things) that a derived class should not hide properties of a base class.
In this case a real number would have to hide the imaginary part of the complex number. It is clear that it makes no sense to store a hidden floating point number (imaginary part) even is you only need the real part.
This is basically the same issue as the rectangle/square example mentioned in a comment.
2
Today I saw this "Liskow Substitution Principle" several times, I'll have to read more about it, because I do not know that.
– Betlista
4 hours ago
6
It is perfectly fine to report the imaginary part of a real number as zero, e.g. through a read-only method. But it makes no sense to implement a real as a complex number where the imaginary part is set to zero. This is exactly a case where inheritance is misleading: while interface inheritance would arguably be fine here, implementation inheritance would result in a problematic design.
– amon
4 hours ago
add a comment |
not a good example of inheritance as absolute value of those two is calculated differently
This isn't actually a compelling reason against all inheritance here, just the proposed class RealNumber
<-> class ComplexNumber
model.
You might reasonably define an interface Number
, which both RealNumber
and ComplexNumber
would implement.
That might look like
interface Number
{
Number Add(Number rhs);
Number Subtract(Number rhs);
// ... etc
}
But then you'd want to constrain the other Number
parameters in these operations to be the same derived type as this
, which you can get close to with
interface Number<T>
{
Number<T> Add(Number<T> rhs);
Number<T> Subtract(Number<T> rhs);
// ... etc
}
Or instead you'd use a language that allowed structural polymorphism, instead of subtype polymorphism. For the specific case of numbers, you might only need the ability to overload arithmetic operators.
complex operator + (complex lhs, complex rhs);
complex operator - (complex lhs, complex rhs);
// ... etc
Number frobnicate<Number>(List<Number> foos, Number bar); // uses arithmetic operations
add a comment |
I would find it totally OK if complex number had a static factory method fromDouble(double)
that would return a complex number with imaginary being zero, so a real number.
But I have trouble seeing why you would want to have a public inherited Real number class. Usually inheritance is used for these reasons (out of my head, correct me if missed some)
extending the behaviour. Real numbers cannot do any extra operations complex number can't do, so no point in doing this.
implementing abstract behaviour with a specific implementation. Since complex number should not be abstract this also does not apply.
code reuse. If you just use the Complex Number you reuse 100% of the code.
more specific/efficient/accurate implementation for a specific task. This could be applied here, real numbers could implement some functionalities faster. But then this subclass should be hidden behind the static
fromDouble(double)
and should not be known outside. This way it would not need to hide the imaginary part. For the outside there should only be complex numbers (which real numbers are). (This assumes the classes are immutable as most number classes.)
It is like implementing a subclass of Integer that is called Zero and hardcodes some of the operations since they are trivial for zero. You could do this, as every zero is an integer, but don't make it public, hide it behind a factory method.
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "131"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsoftwareengineering.stackexchange.com%2fquestions%2f385185%2fhow-to-implement-realnumber-and-complexnumber-inheritance%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Even if in a mathematical sense, a real number is a complex number, it is not a good idea to derive real from complex. It violates the Liskow Substitution Principle saying (among other things) that a derived class should not hide properties of a base class.
In this case a real number would have to hide the imaginary part of the complex number. It is clear that it makes no sense to store a hidden floating point number (imaginary part) even is you only need the real part.
This is basically the same issue as the rectangle/square example mentioned in a comment.
2
Today I saw this "Liskow Substitution Principle" several times, I'll have to read more about it, because I do not know that.
– Betlista
4 hours ago
6
It is perfectly fine to report the imaginary part of a real number as zero, e.g. through a read-only method. But it makes no sense to implement a real as a complex number where the imaginary part is set to zero. This is exactly a case where inheritance is misleading: while interface inheritance would arguably be fine here, implementation inheritance would result in a problematic design.
– amon
4 hours ago
add a comment |
Even if in a mathematical sense, a real number is a complex number, it is not a good idea to derive real from complex. It violates the Liskow Substitution Principle saying (among other things) that a derived class should not hide properties of a base class.
In this case a real number would have to hide the imaginary part of the complex number. It is clear that it makes no sense to store a hidden floating point number (imaginary part) even is you only need the real part.
This is basically the same issue as the rectangle/square example mentioned in a comment.
2
Today I saw this "Liskow Substitution Principle" several times, I'll have to read more about it, because I do not know that.
– Betlista
4 hours ago
6
It is perfectly fine to report the imaginary part of a real number as zero, e.g. through a read-only method. But it makes no sense to implement a real as a complex number where the imaginary part is set to zero. This is exactly a case where inheritance is misleading: while interface inheritance would arguably be fine here, implementation inheritance would result in a problematic design.
– amon
4 hours ago
add a comment |
Even if in a mathematical sense, a real number is a complex number, it is not a good idea to derive real from complex. It violates the Liskow Substitution Principle saying (among other things) that a derived class should not hide properties of a base class.
In this case a real number would have to hide the imaginary part of the complex number. It is clear that it makes no sense to store a hidden floating point number (imaginary part) even is you only need the real part.
This is basically the same issue as the rectangle/square example mentioned in a comment.
Even if in a mathematical sense, a real number is a complex number, it is not a good idea to derive real from complex. It violates the Liskow Substitution Principle saying (among other things) that a derived class should not hide properties of a base class.
In this case a real number would have to hide the imaginary part of the complex number. It is clear that it makes no sense to store a hidden floating point number (imaginary part) even is you only need the real part.
This is basically the same issue as the rectangle/square example mentioned in a comment.
answered 4 hours ago
Frank PufferFrank Puffer
3,83951233
3,83951233
2
Today I saw this "Liskow Substitution Principle" several times, I'll have to read more about it, because I do not know that.
– Betlista
4 hours ago
6
It is perfectly fine to report the imaginary part of a real number as zero, e.g. through a read-only method. But it makes no sense to implement a real as a complex number where the imaginary part is set to zero. This is exactly a case where inheritance is misleading: while interface inheritance would arguably be fine here, implementation inheritance would result in a problematic design.
– amon
4 hours ago
add a comment |
2
Today I saw this "Liskow Substitution Principle" several times, I'll have to read more about it, because I do not know that.
– Betlista
4 hours ago
6
It is perfectly fine to report the imaginary part of a real number as zero, e.g. through a read-only method. But it makes no sense to implement a real as a complex number where the imaginary part is set to zero. This is exactly a case where inheritance is misleading: while interface inheritance would arguably be fine here, implementation inheritance would result in a problematic design.
– amon
4 hours ago
2
2
Today I saw this "Liskow Substitution Principle" several times, I'll have to read more about it, because I do not know that.
– Betlista
4 hours ago
Today I saw this "Liskow Substitution Principle" several times, I'll have to read more about it, because I do not know that.
– Betlista
4 hours ago
6
6
It is perfectly fine to report the imaginary part of a real number as zero, e.g. through a read-only method. But it makes no sense to implement a real as a complex number where the imaginary part is set to zero. This is exactly a case where inheritance is misleading: while interface inheritance would arguably be fine here, implementation inheritance would result in a problematic design.
– amon
4 hours ago
It is perfectly fine to report the imaginary part of a real number as zero, e.g. through a read-only method. But it makes no sense to implement a real as a complex number where the imaginary part is set to zero. This is exactly a case where inheritance is misleading: while interface inheritance would arguably be fine here, implementation inheritance would result in a problematic design.
– amon
4 hours ago
add a comment |
not a good example of inheritance as absolute value of those two is calculated differently
This isn't actually a compelling reason against all inheritance here, just the proposed class RealNumber
<-> class ComplexNumber
model.
You might reasonably define an interface Number
, which both RealNumber
and ComplexNumber
would implement.
That might look like
interface Number
{
Number Add(Number rhs);
Number Subtract(Number rhs);
// ... etc
}
But then you'd want to constrain the other Number
parameters in these operations to be the same derived type as this
, which you can get close to with
interface Number<T>
{
Number<T> Add(Number<T> rhs);
Number<T> Subtract(Number<T> rhs);
// ... etc
}
Or instead you'd use a language that allowed structural polymorphism, instead of subtype polymorphism. For the specific case of numbers, you might only need the ability to overload arithmetic operators.
complex operator + (complex lhs, complex rhs);
complex operator - (complex lhs, complex rhs);
// ... etc
Number frobnicate<Number>(List<Number> foos, Number bar); // uses arithmetic operations
add a comment |
not a good example of inheritance as absolute value of those two is calculated differently
This isn't actually a compelling reason against all inheritance here, just the proposed class RealNumber
<-> class ComplexNumber
model.
You might reasonably define an interface Number
, which both RealNumber
and ComplexNumber
would implement.
That might look like
interface Number
{
Number Add(Number rhs);
Number Subtract(Number rhs);
// ... etc
}
But then you'd want to constrain the other Number
parameters in these operations to be the same derived type as this
, which you can get close to with
interface Number<T>
{
Number<T> Add(Number<T> rhs);
Number<T> Subtract(Number<T> rhs);
// ... etc
}
Or instead you'd use a language that allowed structural polymorphism, instead of subtype polymorphism. For the specific case of numbers, you might only need the ability to overload arithmetic operators.
complex operator + (complex lhs, complex rhs);
complex operator - (complex lhs, complex rhs);
// ... etc
Number frobnicate<Number>(List<Number> foos, Number bar); // uses arithmetic operations
add a comment |
not a good example of inheritance as absolute value of those two is calculated differently
This isn't actually a compelling reason against all inheritance here, just the proposed class RealNumber
<-> class ComplexNumber
model.
You might reasonably define an interface Number
, which both RealNumber
and ComplexNumber
would implement.
That might look like
interface Number
{
Number Add(Number rhs);
Number Subtract(Number rhs);
// ... etc
}
But then you'd want to constrain the other Number
parameters in these operations to be the same derived type as this
, which you can get close to with
interface Number<T>
{
Number<T> Add(Number<T> rhs);
Number<T> Subtract(Number<T> rhs);
// ... etc
}
Or instead you'd use a language that allowed structural polymorphism, instead of subtype polymorphism. For the specific case of numbers, you might only need the ability to overload arithmetic operators.
complex operator + (complex lhs, complex rhs);
complex operator - (complex lhs, complex rhs);
// ... etc
Number frobnicate<Number>(List<Number> foos, Number bar); // uses arithmetic operations
not a good example of inheritance as absolute value of those two is calculated differently
This isn't actually a compelling reason against all inheritance here, just the proposed class RealNumber
<-> class ComplexNumber
model.
You might reasonably define an interface Number
, which both RealNumber
and ComplexNumber
would implement.
That might look like
interface Number
{
Number Add(Number rhs);
Number Subtract(Number rhs);
// ... etc
}
But then you'd want to constrain the other Number
parameters in these operations to be the same derived type as this
, which you can get close to with
interface Number<T>
{
Number<T> Add(Number<T> rhs);
Number<T> Subtract(Number<T> rhs);
// ... etc
}
Or instead you'd use a language that allowed structural polymorphism, instead of subtype polymorphism. For the specific case of numbers, you might only need the ability to overload arithmetic operators.
complex operator + (complex lhs, complex rhs);
complex operator - (complex lhs, complex rhs);
// ... etc
Number frobnicate<Number>(List<Number> foos, Number bar); // uses arithmetic operations
answered 4 hours ago
CalethCaleth
5,69911320
5,69911320
add a comment |
add a comment |
I would find it totally OK if complex number had a static factory method fromDouble(double)
that would return a complex number with imaginary being zero, so a real number.
But I have trouble seeing why you would want to have a public inherited Real number class. Usually inheritance is used for these reasons (out of my head, correct me if missed some)
extending the behaviour. Real numbers cannot do any extra operations complex number can't do, so no point in doing this.
implementing abstract behaviour with a specific implementation. Since complex number should not be abstract this also does not apply.
code reuse. If you just use the Complex Number you reuse 100% of the code.
more specific/efficient/accurate implementation for a specific task. This could be applied here, real numbers could implement some functionalities faster. But then this subclass should be hidden behind the static
fromDouble(double)
and should not be known outside. This way it would not need to hide the imaginary part. For the outside there should only be complex numbers (which real numbers are). (This assumes the classes are immutable as most number classes.)
It is like implementing a subclass of Integer that is called Zero and hardcodes some of the operations since they are trivial for zero. You could do this, as every zero is an integer, but don't make it public, hide it behind a factory method.
add a comment |
I would find it totally OK if complex number had a static factory method fromDouble(double)
that would return a complex number with imaginary being zero, so a real number.
But I have trouble seeing why you would want to have a public inherited Real number class. Usually inheritance is used for these reasons (out of my head, correct me if missed some)
extending the behaviour. Real numbers cannot do any extra operations complex number can't do, so no point in doing this.
implementing abstract behaviour with a specific implementation. Since complex number should not be abstract this also does not apply.
code reuse. If you just use the Complex Number you reuse 100% of the code.
more specific/efficient/accurate implementation for a specific task. This could be applied here, real numbers could implement some functionalities faster. But then this subclass should be hidden behind the static
fromDouble(double)
and should not be known outside. This way it would not need to hide the imaginary part. For the outside there should only be complex numbers (which real numbers are). (This assumes the classes are immutable as most number classes.)
It is like implementing a subclass of Integer that is called Zero and hardcodes some of the operations since they are trivial for zero. You could do this, as every zero is an integer, but don't make it public, hide it behind a factory method.
add a comment |
I would find it totally OK if complex number had a static factory method fromDouble(double)
that would return a complex number with imaginary being zero, so a real number.
But I have trouble seeing why you would want to have a public inherited Real number class. Usually inheritance is used for these reasons (out of my head, correct me if missed some)
extending the behaviour. Real numbers cannot do any extra operations complex number can't do, so no point in doing this.
implementing abstract behaviour with a specific implementation. Since complex number should not be abstract this also does not apply.
code reuse. If you just use the Complex Number you reuse 100% of the code.
more specific/efficient/accurate implementation for a specific task. This could be applied here, real numbers could implement some functionalities faster. But then this subclass should be hidden behind the static
fromDouble(double)
and should not be known outside. This way it would not need to hide the imaginary part. For the outside there should only be complex numbers (which real numbers are). (This assumes the classes are immutable as most number classes.)
It is like implementing a subclass of Integer that is called Zero and hardcodes some of the operations since they are trivial for zero. You could do this, as every zero is an integer, but don't make it public, hide it behind a factory method.
I would find it totally OK if complex number had a static factory method fromDouble(double)
that would return a complex number with imaginary being zero, so a real number.
But I have trouble seeing why you would want to have a public inherited Real number class. Usually inheritance is used for these reasons (out of my head, correct me if missed some)
extending the behaviour. Real numbers cannot do any extra operations complex number can't do, so no point in doing this.
implementing abstract behaviour with a specific implementation. Since complex number should not be abstract this also does not apply.
code reuse. If you just use the Complex Number you reuse 100% of the code.
more specific/efficient/accurate implementation for a specific task. This could be applied here, real numbers could implement some functionalities faster. But then this subclass should be hidden behind the static
fromDouble(double)
and should not be known outside. This way it would not need to hide the imaginary part. For the outside there should only be complex numbers (which real numbers are). (This assumes the classes are immutable as most number classes.)
It is like implementing a subclass of Integer that is called Zero and hardcodes some of the operations since they are trivial for zero. You could do this, as every zero is an integer, but don't make it public, hide it behind a factory method.
answered 23 mins ago
finduslfindusl
1193
1193
add a comment |
add a comment |
Thanks for contributing an answer to Software Engineering Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsoftwareengineering.stackexchange.com%2fquestions%2f385185%2fhow-to-implement-realnumber-and-complexnumber-inheritance%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
4
this looks like covered in prior question: Should rectangle inherit from square?
– gnat
5 hours ago
1
@gnat Oh man, that was another example I wanted to use... Thanks!
– Betlista
4 hours ago
2
... Note that the sentence "real number is a complex number" in the mathematical sense is only valid for immutable numbers, so if you use immutable objects, you can avoid the LSP violation (same holds also for squares and rectangles, see this SO answer).
– Doc Brown
3 hours ago
3
... Note further the absolute value calculation for complex numbers works also for real numbers, so I am not sure what your professor meant. If you implement an "Abs()" method correctly in an immutable complex number and derive a "real" from it, the Abs() method will still deliver correct results.
– Doc Brown
3 hours ago
2
Possible duplicate of Should rectangle inherit from square?
– BobDalgleish
1 hour ago