In oop what is the difference between a subclass and a subtype?
Printable View
In oop what is the difference between a subclass and a subtype?
I haven't really heard the word subtype used too much but I don't think there is a difference.
Hello,
A subtype is a specialized version of a type. In C++ a derived class is a subtype (subclass) of a base class type. A variable of the derived type could appear anywhere that a variable of the parent type is requested.
Characteristics of a subclass that guarantees it is a subtype:
» Can only add variables and methods or redefine existing methods in a compatible way.
If the subclass hides some of the parent’s variables and functions or modifies them in an incompatible way, then it does not create a subtype.
What is a compatible way?
» By imposing some restrictions on the use of inheritance, the language can ensure substitutability.
If you would like more information, check out the following link: Subtyping vs. Subclassing
- Stack Overflow
When you use "subclass" do you mean the MFC and winapi method of subclassing a control? A subtype of another type means you can use the subtype in all situations where the parent type could be used. With C+, you not only have the subtype relation between two class(which should occur with proper inheritance trees), but also have protected and private inheritance.