One time definition of a generic base class with recursive type specifier. Each node has one parent and multiple children.
The above can be re-used every time a tree hierarchy of objects needs to be defined. The node object in the tree has to inherit from the base class with
each node class knows where it is in the hierarchy, what the parent object is as well as what the children objects are. Several built in types use a tree structure, like Control or XmlElement and the above Tree<T> can be used as a base class of any type in your code.
For example, to create a hierarchy of parts where the total weight is calculated from the weight of all the children, do the following:
to be used as
Another example would in the definition of relative coordinate frames. In this case the true position of the coordinate frame depends on the positions of all the parent coordinate frames.
to be used as
Constructors In A Subclass
When you make a subclass of a base class, you can construct the base class by using : base after the subclass constructor's parameters.
Extending an abstract base class
Unlike interfaces, which can be described as contracts for implementation, abstract classes act as contracts for extension.
An abstract class cannot be instantiated, it must be extended and the resulting class (or derived class) can then be instantiated.
Abstract classes are used to provide generic implementations
The above example shows how any class extending Car will automatically receive the HonkHorn method with the implementation. This means that any developer creating a new Car will not need to worry about how it will honk it's horn.
Lets say there are 2 classes class Foo and Bar. Foo has two features Do1 and Do2. Bar needs to use Do1 from Foo, but it doesn't need Do2 or needs feature that is equivalent to Do2 but does something completely different.
Bad way: make Do2() on Foo virtual then override it in Bar or just throw Exception in Bar for Do2()
Take out Do1() from Foo and put it into new class Baz then inherit both Foo and Bar from Baz and implement Do2() separately
Now why first example is bad and second is good: When developer nr2 has to do a change in Foo, chances are he will break implementation of Bar because Bar is now inseparable from Foo. When doing it by latter example Foo and Bar commonalty has been moved to Baz and they do not affect each other (like the shouldn't).
Inheritance. Constructors' calls sequence
Consider we have a class Animal which has a child class Dog
By default every class implicitly inherits the Object class.
This is same as the above code.
When creating an instance of Dog class, the base classes's default constructor (without parameters) will be called if there is no explicit call to another constructor in the parent class. In our case, first will be called Object's constructor, then Animal's and at the end Dog's constructor.
In the above examples, our Dog class constructor calls the default constructor of the Animal class. If you want, you can specify which constructor should be called: it is possible to call any constructor which is defined in the parent class.
Consider we have these two classes.
What is going here?
We have 2 constructors in each class.
What does base mean?
base is a reference to the parent class. In our case, when we create an instance of Dog class like this
The runtime first calls the Dog(), which is the parameterless constructor. But its body doesn't work immediately. After the parentheses of the constructor we have a such call: base(), which means that when we call the default Dog constructor, it will in turn call the parent's default constructor. After the parent's constructor runs, it will return and then, finally, run the Dog() constructor body.
Now what if we call the Dog's constructor with a parameter?
You know that members in the parent class which are not private are inherited by the child class, meaning that Dog will also have the name field.
In this case we passed an argument to our constructor. It in his turn passes the argument to the parent class' constructor with a parameter, which initializes the name field.
Output will be
Animal's constructor with 1 parameter
Dog's constructor with 1 parameter
Every object creation starts from the base class. In the inheritance, the classes which are in the hierarchy are chained. As all classes derive from Object, the first constructor to be called when any object is created is the Object class constructor; Then the next constructor in the chain is called and only after all of them are called the object is created
The base keyword is used to access members of the base class from within a derived class:
Call a method on the base class that has been overridden by another method.
Specify which base-class constructor should be called when creating instances of the derived class.
Inheriting from a base class
To avoid duplicating code, define common methods and attributes in a general class as a base:
Now that you have a class that represents Animal in general, you can define a class that describes the peculiarities of specific animals:
The Cat class gets access to not only the methods described in its definition explicitly, but also all the methods defined in the general Animal base class. Any Animal (whether or not it was a Cat) could Eat, Stare, or Roll. An Animal would not be able to Scratch, however, unless it was also a Cat. You could then define other classes describing other animals. (Such as Gopher with a method for destroying flower gardens and Sloth with no extra methods at all.)
Inheriting from a class and implementing an interface
Inheriting from a class and implementing multiple interfaces
There are several ways methods can be inherited
Testing and navigating inheritance
This modified text is an extract of the original Stack Overflow Documentation created by following contributors and released under CC BY-SA 3.0