Object-oriented programming: Difference between revisions

Content deleted Content added
Antonielly (talk | contribs)
Undid revision 264254000 by Nitrambarrell (talk). Rv unexplained removal of valid text
Fundamental concepts: same order for example as for general tet
Line 21:
; [[Inheritance (computer science)|Inheritance]] : "Subclasses" are more specialized versions of a class, which ''inherit'' attributes and behaviors from their parent classes, and can introduce their own.
: For example, the class <code>Dog</code> might have sub-classes called <code>Collie</code>, <code>Chihuahua</code>, and <code>GoldenRetriever</code>. In this case, <code>Lassie</code> would be an instance of the <code>Collie</code> subclass. Suppose the <code>Dog</code> class defines a method called <code>bark()</code> and a property called <code>furColor</code>. Each of its sub-classes (<code>Collie</code>, <code>Chihuahua</code>, and <code>GoldenRetriever</code>) will inherit these members, meaning that the programmer only needs to write the code for them once.
: Each subclass can alter its inherited traits. For example, the <code>Collie</code> class might specify that the default <code>furColor</code> for a collie is brown-and-white. The <code>Chihuahua</code> subclass might specify that the <code>bark()</code> method produces a high pitch by default. Subclasses can also add new members. The <code>Chihuahua</code> subclass could add a method called <code>tremble()</code>. So an individual chihuahua instance would use a high-pitched <code>bark()</code> from the <code>Chihuahua</code> subclass, which in turn inherited the usual <code>bark()</code> from <code>Dog</code>. The chihuahua object would also have the <code>tremble()</code> method, but <code>Lassie</code> would not, because she is a <code>Collie</code>, not a <code>Chihuahua</code>. In fact, inheritance is an "'''''a''... is a'''" relationship between classes, while instantiation is an "'''is a'''" relationship between an object and a class: '''''a''''' <code>LassieCollie</code> ''is a'' <code>CollieDog</code> ("a... is a"), but '''''a''''' <code>CollieLassie</code> ''is a'' <code>DogCollie</code> ("a... is a"). Thus, the object named <code>Lassie</code> has the methods from both classes <code>Collie</code> and <code>Dog</code>.
: [[Multiple inheritance]] is inheritance from more than one ancestor class, neither of these ancestors being an ancestor of the other. For example, independent classes could define <code>Dog</code>s and <code>Cat</code>s, and a <code>Chimera</code> object could be created from these two which inherits all the (multiple) behavior of cats and dogs. This is not always supported, as it can be hard both to implement and to use well.
; [[Abstraction (computer science)|Abstraction]] : Abstraction is simplifying complex reality by modelling classes appropriate to the problem, and working at the most appropriate level of inheritance for a given aspect of the problem.