The scripting languages that Unity supports
have a feature called Inheritance.
Inheritance is one of the cornerstones
of Object-Oriented Programming, or OOP for short.
When a class inherits from another,
it gains the features of the class it inherits from.
When talking about inheritance, the class that is
being inherited from is called the "Parent", or base class.
The class that is inheriting is called the "Child" or derived class.
The result of inheritance is that items that exist in the parent class
will also be available in the child class.
Therefore, methods and variables can be used
in the child class as if it was the parent class.
For example, assume you have a parent class called "Class A",
which contains two methods: Dance() and Sing().
If you have another class, "Class B", which inherits
from Class A, then Class B will also have the two methods,
Dance() and Sing().
These methods do not need to be created in Class B
because they already exist in Class A.
When dealing with inheritance, there are three
access modifiers to be aware of: Public, Private, and Protected.
You should already be familiar with the concepts
of the Public and the Private access modifiers.
Just be aware that the features of a parent class that are Public
will exist, and be accessible, in the child class,
while features that are Private will exist ,
but not be accessible, in the child class.
The Protected access modifier acts as a hybrid
between Public and Private.
All features of a parent class that are Protected
will exist and be accessible in a child class,
like Public features, but will not be accessible outside
of the parent or child classes, like Private features.
It is likely that most of the classes
you have used so far in Unity have been inheriting.
Indeed, all scripts which are applied as components
to GameObjects are MonoBehaviours.
This means they have inherited from the MonoBehaviour class.
By default, scripts made in Unity follow this format:
"public class", followed by the name of the class,
followed by a colon, and the class name: MonoBehaviour.
The colon in class name, MonoBehaviour, is telling
the script that it inherits from MonoBehaviour.
To make this class inherit from another class, simply
change the name MonoBehaviour to some other class name.
To change the class so it doesn't inherit from
any parent class, simply remove the colon and parent class name.
You might be wondering why our scripts inherit from MonoBehaviour.
Items like GameObject, Transform,
the Start() method, the Update() method,
and more, all come from MonoBehaviour.
Our scripts inherit from MonoBehaviour
so that we have access to these features.
The inheritance structure is hierarchical.
A common way to think of inheritance is to think of the animal kingdom.
In this example, we'd have a parent class called "Animal".
This class would contain all of the definitions and properties
necessary to make the class behave like an animal.
>From this Animal base class, we might have a couple of child classes:
Vertebrate, and Invertebrate.
The Vertebrate class would then, in turn,
be the parent class for more classes,
such as Mammal, Reptile, or Amphibian.
Each of these child classes would take the information given
by its base class, and add to it.
Just like our animal example, inheritance
in Object-Oriented Programming
is known as an Is-A relationship.
This means that the child class is a parent class.
A Reptile is a Vertebrate.
A Mammal is an Animal.
An example in Unity that you may have come across before is:
A Capsule Collider is a Collider.
This concept will be covered further in the lesson on
Polymorphism, linked below.
The idea of inheritance can be very useful, and applicable,
in game development.
We might, for example, have a class called "Humanoid".
This class covers all of the things that humanoids should do in our game.
We then have two child classes: Enemy and Player.
These control the specifics of how players
and enemies work in the game while already behaving
like humanoids, because they've inherited
all of the Humanoid class's members.
We could then have two more child classes of Enemy:
Orc and Goblin.
These already behave like enemies,
which in turn behave like humanoids.
In this way, we have much less code to write
to make Orcs and Goblins behave
as we want them to, because we're reusing
the code from Humanoid and Enemy.
Constructors are an exception to what is inherited by child classes,
as they remain unique to a class, and are never shared.
When a constructor is called in a child class, however,
the constructor of its parent class is called immediately before.
Since classes can have many different constructors,
we might want to be able to control
which base class constructor is being called.
We do this with the keyword "base".
By following the parameter list
of the child's constructor with a colon,
you can explicitly call a specific constructor
of the base class, using the keyword "base",
in the base constructor's parameter list.
If the base class's constructor is not called explicitly,
then the default constructor will still be called implicitly.
Aside from calling the base class's constructor,
you can also use the base keyword to access
other members of the base class.
This is useful in situations where you wish to access the base class's
version of something, because it is different than the derived version.
This happens often when overriding functions.
For more information on this, see the lesson on Overriding linked below.