# F Sharp Programming/Inheritance

Many object-oriented languages use inheritance extensively in the .NET BCL to construct class hierarchies.

## Subclasses

A subclass is, in the simplest terms, a class derived from a class which has already been defined. A subclass inherits its members from a base class in addition to adding its own members. A subclass is defined using the inherit keyword as shown below:
Our simple class hierarchy looks like this:
The Student and Worker subclasses both inherit the Name and Greet methods from the Person base class. This can be demonstrated in fsi:
.NET's object model supports single-class inheritance, meaning that a subclass is limited to one base class. In other words, its not possible to create a class which derives from Student and Employee simultaneously.

### Overriding Methods

Occasionally, you may want a derived class to change the default behavior of methods inherited from the base class. For example, the output of the .ToString method above isn't very useful. We can override that behavior with a different implementation using the override:
We've overridden the default implementation of the ToString method, causing it to print out a person's name.
Methods in F# are not overridable by default. If you expect users will want to override methods in a derived class, you have to declare your method as overridable using the abstract and default keywords as follows:
Our class Person provides a Greet method which may be overridden in derived classes. Here's an example of these two classes in fsi:

### Abstract Classes

An abstract class is one which provides an incomplete implementation of an object, and requires a programmer to create subclasses of the abstract class to fill in the rest of the implementation. For example, consider the following:
The first thing you'll notice is the AbstractClass attribute, which tells the compiler that our class has some abstract members. Additionally, you notice two abstract members, Draw and Area don't have an implementation, only a type signature.
We can't create an instance of Shape because the class hasn't been fully implemented. Instead, we have to derive from Shape and override the Draw and Area methods with a concrete implementation:
Now we have several different implementations of the Shape class. We can experiment with these in fsi:

## Working With Subclasses
### Up-casting and Down-casting

A type cast is an operation which changes the type of an object from one type to another. This is not the same as a map function, because a type cast does not return an instance of a new object, it returns the same instance of an object with a different type.
For example, let's say B is a subclass of A. If we have an instance of B, we are able to cast as an instance of A. Since A is upward in the class hierarchy, we call this an up-cast. We use the :> operator to perform upcasts:
Up-casting is considered "safe", because a derived class is guaranteed to have all of the same members as an ancestor class. We can, if necessary, go in the opposite direction: we can down-cast from an ancestor class to a derived class using the :?> operator:
Since intAsObj holds an int boxed as an obj, we can downcast to an int as needed. However, we cannot downcast to a string because it is an incompatible type. Down-casting is considered "unsafe" because the error isn't detectable by the type-checker, so an error with a down-cast always results in a runtime exception.

#### Up-casting example

This program has the following types:
This program outputs: