views:

314

answers:

6

In set theory, a set is a superset if it contains everything in the original set and possibly more. A subset however is does not contain everything of the initial set.

With that in mind, in most object-oriented programming languages (I'm thinking Objective-C but I know the same is true for Java and others), the parent class is called the super class, and any class which inherits from the super is called a subclass.

Isn't this backwards? A subclass inherits things like all instance variables and methods from its superclass, thus it "contains" everything from the parent, plus whatever is added in the subclass. Is this just a naming mistake or was this intentional, and if so why?

+15  A: 

A superclass defines a class that has a larger set of possible values as members. A subclass restricts the items that can be part of its class, so it defines a smaller set of possible members.

The set of possible members of a superclass is a superset of the set of possible members of a subclass of that superclass.

Greg Hewgill
If I have a class called `Parent`, and it has members `A`, `B`, and `C`, and then I subclass it naming the subclass `Child`, doesn't `Child` automatically get `A`, `B`, and `C`, plus `D` `E` and `F` if I so choose? Doesn't this mean `Child` is in a sense a superset of `Parent`?
jbrennan
But the *set* of Parents (the superclass) is a *superset* of the set of Children; the set of Children (the subclass) is a *subset* of the set of Parents. "Members" is referring to the set of objects that are members of the type, not the set of member variables.
itowlson
jbrennan: You shouldn't think of a class as a set of its properties. You should think of it as a set of all its possible instances. Then it will make sense. Just like in maths Z is a strict superset of N, because all natural numbers are whole numbers, but not all whole numbers are natural numbers, Object is the superclass of String in java because all strings are objects, but not all objects are strings.
sepp2k
@jbrennan: You're thinking about *attributes* of class, not possible *values* of that type (in set terms, members of the set of values defined by the class). The set of all possible `Shape` values contains some values that are circles and some values that are squares, but `Circle` and `Square` could be classes that have a much smaller set off possible members than a general `Shape`. (Remember I am using the term "member" as "member of the set of values" instead of the programming term "class member".)
Greg Hewgill
Suppose there is another `GrandChild` class which extends `Child` and adds `G` `H` fields. `GrandChild` and `Child` could also be interpret as `Parent` in OO term, while it's only `GrandChild` for `Child` in the same case. Therefore `Parent` is *super* in this sense.
m3rLinEz
while it's a nice explanation, i get the feeling this was plucked from the air to retrospectively explain why it turned out this way :P. any chance of some references?
Matt Joiner
@Matt - I get that feeling too! It is interesting that they would put `SubClass extends SuperClass`.
fastcodejava
@Matt: it's straight set theory - but it's all about the _instances_, not the properties and methods.
Steven A. Lowe
@Steven: Amusing, I'm currently studying set theory. Thanks for the suggestion.
Matt Joiner
+1  A: 

I sidestep the whole super/sub class issue and refer to them as "derived" and "parent" class.

James McLeod
this might just be the more practical approach
Matt Joiner
+2  A: 

Probably for the same reason that stacks grow down (bottom at the top), trees grow down (root at the top) and 2D graphics systems are almost always quadrant IV (0,0 in upper left).

Southern Hospitality
So the next question would be what that reason is :-)
DerMike
+4  A: 

Greg is correct. Two things to consider that might make it more clear:

  1. the propertes and methods are not relevant to the sub/super relationship in terms of set theory:

    • the properties and methods defined by a subclass may extend beyond those provided by its superclass (and in fact, they often do), but instances of the subclass are still members of the set of instances of the superclass
    • in other words, the sub/super relationship is not defined by the propertes and methods, but by the instance-level semantics intended by the naming of the classes
  2. Taxomony example:

    • the set of all People is larger than the set of all Programmers
    • the set People is, in fact, a superset of the set Programmers
    • the set Programmers is a subset of the set People

so in OOP terms, People would be a superclass and Programmer would be a subclass. Every Programmer is a person, but not every person is a Programmer. Hence superclass and subclass. The fact that the Programmer class may have super powers beyond the ken of mortal men does not change the class-relationship (is-a) semantics.

Steven A. Lowe
+1  A: 
Norman Ramsey
C++: base class, derived class, problem solved ;-)
Steve Jessop
+1  A: 

Greg's answer is correct. Here's an explanation by example:

You have a base class Base. You have two derived classes DerivedA and DerivedB. Every instance of DerivedA is also an instance of Base. Likewise, every DerivedB is also a Base. But, a DerivedA is not a DerivedB and vice versa. So, if you were to draw a Venn diagram for your objects, you'd get:

    ________________________
   /                        \
  /          Base            \
 /    ______        ______    \
|    /      \      /      \    |
|   /        \    /        \   |
|  | DerivedA |  | DerivedB |  |
|   \        /    \        /   |
|    \______/      \______/    |
 \                            /
  \                          /
   \________________________/

In other words, every object in the set of DerivedA objects is also in the set of Base objects. Likewise for DerivedB. So Base is indeed the superset of both DerivedA and DerivedB. Hence, it is the "superclass".

munificent