# Object-Oriented Programming/Inheritance

This lesson introduces inheritance.

## Objectives and Skills

Objectives and skills for this lesson include:
* 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.

## Readings
#### General

* Wikipedia: Inheritance (object-oriented programming)
* Wikipedia: Class diagram

#### C#

* W3Schools: C# Inheritance

#### Java

* Javatpoint: Inheritance in Java

#### Python

* OverIQ: Python Inheritance and Polymorphism
* Programiz: Python Inheritance

## Multimedia
#### General

* YouTube: Identifying an inheritance situation
* YouTube: Object Oriented Inheritance Principle
* YouTube: UML Quick Review

#### JavaScript

* YouTube: Javascript - Classes and Inheritance

#### Python

* YouTube: Using inheritance in Python

## Examples

* 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)
* C#
* Java
* JavaScript
* Python3

## Activities

* 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.

## Lesson Summary

* 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.
* 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.
* 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.
* Subtyping enables a given type to be substituted for another type or abstraction.
* 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.
* In some languages a class may be declared as uninheritable by adding certain class modifiers to the class declaration.
** Examples include the "final" keyword in Java and C++ 11 onwards, or the "sealed" keyword in C#.
* 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).
** 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.
* 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.
* 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.
** The inheritance hierarchy of an object is fixed at instantiation when the object's type is selected and does not change with time.
** Using single inheritance, a subclass can inherit from only one superclass.
* Some programmers argue that the problem with implementation inheritance is that it introduces unnecessary coupling in the form of the "fragile base class problem".
** Modifications to the base class implementation can cause inadvertent behavioral changes in subclasses.
* Design constraints as a result of over implementing inheritance:
** Singleness - Using single inheritance, a subclass can inherit from only one superclass.
** Static - The inheritance hierarchy of an object is fixed at instantiation when the object's type is selected and does not change with time.
** 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.
* 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.

## Key Terms
### inheritance

    The mechanism of basing an object or class upon another object or class while retaining similar implementation.

### 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.

### multilevel inheritance

    Occurs when a subclass is inherited from another subclass.

### multiple inheritances

    A feature of classes that allow them to have more than one superclass and inherit features from all parent classes.

### single inheritance

    A type of inheritance where subclasses inherit the features of one superclass.

### singleness

    A design restriction for subclasses in which subclasses can inherit only from one superclass.

### 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.

### subclass

    A modular, derivative class that inherits traits from its parent class (or superclass).

### visibility

    A design constraint where whenever client code has access to an object, it generally has access to all the object's superclass data.

## See Also

* Python Programming/Classes
* hws.edu: Inheritance, Polymorphism, and Abstract Classes
* Github: The Secret Life of Objects: Inheritance
* StackExchange: Why would Square inheriting from Rectangle be problematic if we override the SetWidth and SetHeight methods?
* Adobe: Object-Oriented Programming Concepts: Inheritance
* UML Class Diagram
* Javascript: Subclasses / Inheritance
* PyNative: Python Object-Oriented Programming Exercises