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?
Yes, Java allows this.
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.
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.
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.
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 is correct. Two things to consider that might make it more clear:
the propertes and methods are not relevant to the sub/super relationship in terms of set theory:
Taxomony example:
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With