This lesson introduces inheritance.
Objectives and Skills
edit
Objectives and skills for this lesson include:[1]
- Be able to create a UML diagram
- Understand inheritance
- Inheriting the functionality of a base class into a derived class
- Calling base class functionality from subclasses.
Square (Polygon)
|
+ area: float {read only}
|
|
Rectangle (Polygon)
|
+ area: float {read only}
|
|
Triangle (Polygon)
|
+ area: float {read only}
|
|
Polygon
|
+ perimeter: float {read only}
|
+ Polygon(sides: [float]) - validate_side(side: float)
|
- Prepare UML class diagrams and create an object-oriented program that demonstrates inheritance based on one of the following:
- Computer hardware: include input, output, and storage devices
- Employees: include managers (salary), sales (commission), and hourly employees
- Pets: include mammals, birds, and either fish or reptiles
- Rooms: include bathroom, family room or living room, and kitchen
- Vehicles: include cars, SUVs, and trucks
- Video game characters: include fighters, monsters, and either wizards or healers
- Include at least one property and one method in the base class to be used by all subclasses.
- Include at least one property and one method unique to each subclass.
- Include program, class, and method documentation consistent with the documentation standards for your selected programming language.
- Add unit testing to test all aspects of each class. Run tests automatically when the class module itself is run as the main program.
- In most class-based object-oriented languages, an object created through inheritance (a child object) acquires all the properties and behaviors of the parent object, except for constructors, destructors, overloaded operators and friend functions of the base class.[2]
- Inheritance allows programmers to create classes that are built upon existing classes to specify a new implementation while maintaining the same behaviors (realizing an interface), reuse code, and independently extend original software via public classes and interfaces.[3]
- Inheritance should not be confused with subtyping. In some languages, inheritance and subtyping agree, whereas in others they differ; in general, subtyping establishes an is-a relationship, whereas inheritance only reuses implementation and establishes a syntactic relationship, not necessarily a semantic relationship.[4]
- Subtyping enables a given type to be substituted for another type or abstraction.[5]
- A subclass, "derived class", heir class, or child class is a modular, derivative class that inherits one or more language entities from one or more other classes (called a superclass, base class, or parent class). The semantics of class inheritance vary from language to language, but commonly the subclass automatically inherits the instance variables and member functions of its superclass.[6]
- In some languages a class may be declared as uninheritable by adding certain class modifiers to the class declaration. [7]
- Examples include the "final" keyword in Java and C++ 11 onwards, or the "sealed" keyword in C#.[8]
- Just as classes may be sealed/finalized, method declarations may contain method modifiers that prevent the method from being overridden (i.e. replaced with a new function with the same name and type signature in a subclass).[9]
- A private method is unoverridable simply because it is not accessible by classes other than the class it is a member function of (although this is not true for C++). A "final" method in Java, a "sealed" method in C#, or a frozen feature in Eiffel cannot be overridden.[10]
- Implementation inheritance is the mechanism whereby a subclass reuses code in a base class. By default, the subclass retains all of the operations of the base class, but the subclass may override some or all operations, replacing the base-class implementation with its own.[11]
- Implementing inheritance creates some restrictions in design:
- Whenever client code has access to an object, it generally has access to all the object's superclass data. Even if the superclass has not been declared public, the client can still cast the object to its superclass type.[12]
- The inheritance hierarchy of an object is fixed at instantiation when the object's type is selected and does not change with time.[13]
- Using single inheritance, a subclass can inherit from only one superclass.[14]
- Some programmers argue that the problem with implementation inheritance is that it introduces unnecessary coupling in the form of the "fragile base class problem". [15]
- Modifications to the base class implementation can cause inadvertent behavioral changes in subclasses.[16]
- Design constraints as a result of over implementing inheritance:
- Singleness - Using single inheritance, a subclass can inherit from only one superclass. [17]
- Static - The inheritance hierarchy of an object is fixed at instantiation when the object's type is selected and does not change with time. [18]
- Visibility - There is no way to give a function a pointer without also giving that function access to all of the personal data stored in the superclass. [19]
- The composite reuse principle is an alternative to inheritance. It supports polymorphism and code reuse by separating behaviors from the primary class hierarchy and including specific behavior classes as required in any business domain class. This approach avoids the static nature of a class hierarchy by allowing behavior modifications at run time and allows one class to implement behaviors buffet-style, instead of being restricted to the behaviors of its ancestor classes.[20]
- inheritance
- The mechanism of basing an object or class upon another object or class while retaining similar implementation.[21]
- method overriding
- A language feature that allows a child class to provide a specific implementation of a method that is already provided by one of its parent classes.[22]
- multilevel inheritance
- Occurs when a subclass is inherited from another subclass.[23]
- multiple inheritances
- A feature of classes that allow them to have more than one superclass and inherit features from all parent classes.[24]
- single inheritance
- A type of inheritance where subclasses inherit the features of one superclass.[25]
- singleness
- A design restriction for subclasses in which subclasses can inherit only from one superclass.[26]
- static
- A design restriction in which the hierarchy of an object is fixed at instantiation when the object's type is selected. The object's type does not change with time.[27]
- subclass
- A modular, derivative class that inherits traits from its parent class (or superclass).[28]
- visibility
- A design constraint where whenever client code has access to an object, it generally has access to all the object's superclass data.[29]