Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are Super-class and Sub-class reversed?

Tags:

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?

like image 227
jbrennan Avatar asked Feb 21 '10 00:02

jbrennan


People also ask

Can a class be both a superclass and a subclass?

Yes, Java allows this.

Is a relationship between a super class and sub class?

Definition: A subclass is a class that derives from another class. A subclass inherits state and behavior from all of its ancestors. The term superclass refers to a class's direct ancestor as well as all of its ascendant classes.

What is the difference between super and sub class?

The difference between the Superclass and Subclass is that Superclass is the existing class from which new classes are derived while Subclass is the new class that inherits the properties and methods of the Superclass.

What is the best example of a super class and sub class relationship?

A subclass is a class derived from the superclass. It inherits the properties of the superclass and also contains attributes of its own. An example is: Car, Truck and Motorcycle are all subclasses of the superclass Vehicle.


2 Answers

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.

like image 114
Greg Hewgill Avatar answered Dec 27 '22 11:12

Greg Hewgill


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.

like image 39
Steven A. Lowe Avatar answered Dec 27 '22 11:12

Steven A. Lowe