Jump to content

Subclass (computer science)

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Lachatdelarue (talk | contribs) at 14:34, 29 September 2004 (fixing wikilink). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

In object-oriented programming, a subclass is a class that inherits some properties from its superclass.

You can usually think of the subclass as being "a kind of" its superclass, as in a "a Manx is a kind of cat", or "a square is a kind of rectangle".

cat rectangle
has fur
has four legs
has a tail
has four sides, lengths w and h
manx catsquare
does not have a tailall sides have same length, w=b

This table aims to show how subclasses (below) are like their superclasses (above) but more specific. That is, the facts about the cat states what is generally true for all cats, even if some kinds of cats happen to have no tail.


Don't confuse the subclass-superclass relationship with that of classes and instances. An "instance of cat" refers to one particular cat. The manx cat in the table is still a class - there are many instances of manx cats. And if a particular cat (an instance of the cat class) happen to have its tail bitten off by a fox, that does not change the cat class. It's just that particular cat that has changed.


See also