Beginning of dialog window. Escape will cancel and close the window.
End of dialog window.
English
00:00:01
- [Narrator] The scripting languages
00:00:02
that Unity supports have a
feature called Inheritance.
00:00:05
Inheritance is one of the cornerstones
00:00:06
of objected oriented programming,
00:00:08
or OOP for short.
00:00:11
When a class inherits from another,
00:00:12
it gains the features of
the class it inherits from.
00:00:16
When talking about inheritance,
00:00:17
the class that is being inherited from
00:00:19
is called the parent or base class.
00:00:21
The class that is inheriting is called
00:00:23
the child or derived class.
00:00:26
The result of inheritance is that items
00:00:28
that exist in the parent class
00:00:30
will also be available in the child class,
00:00:32
therefore, methods and variables
00:00:35
can be used in the child class as if
00:00:37
it was the parent class.
00:00:39
For example, assume you have
a parent class called Class A,
00:00:42
which contains two
methods, dance and sing.
00:00:47
If you have another class, Class B,
00:00:49
which inherits from Class A, then Class B
00:00:52
will also have the two
methods, dance and sing.
00:00:55
These methods do not need to be created
00:00:57
in Class B because they
already exist in Class A.
00:01:02
When dealing with inheritance,
00:01:03
there are three access
modifiers to be aware of,
00:01:06
public, private, and protected.
00:01:10
You should already be familiar
00:01:11
with the concepts of the public
00:01:12
and the private access modifiers.
00:01:15
Just be aware that the features
00:01:16
of a parent class that are public
00:01:18
will exist and be accessible
in the child class,
00:01:21
while features that are private
00:01:23
will exist but not be
accessible in the child class.
00:01:27
The protected access modifier acts
00:01:28
as a hybrid between public and private.
00:01:31
All features of a parent class
00:01:32
that are protected will
exist and be accessible
00:01:34
in a child class, like public features,
00:01:37
but will not be accessible outside
00:01:38
of the parent or child
classes, like private features.
00:01:42
It is likely that most of the classes
00:01:44
you have used so far in
Unity have been inheriting.
00:01:47
Indeed, all scripts which are applied
00:01:49
as components to game
objects are Monobehaviors.
00:01:53
This means they have inherited from
00:01:54
the Monobehavior class.
00:01:56
By default, scripts made in
Unity follow this format.
00:02:00
Public class followed by
the name of the class,
00:02:03
followed by a colon and the
class name Monobehavior.
00:02:07
The colon in class name, Monobehavior,
00:02:10
is telling the script that it
inherits from Monobehavior.
00:02:13
To make this class inherit
from another class,
00:02:15
simply change the name Monobehavior
00:02:17
to some other class name.
00:02:19
To change the class so
that it doesn't inherit
00:02:21
from any parent class,
simply remove the colon
00:02:24
and parent class name.
00:02:26
You might be wondering
why our scripts inherit
00:02:28
from Monobehavior.
00:02:29
Items like game object, transform,
00:02:32
the start method, the update method,
00:02:35
and more, all come from Monobehavior.
00:02:38
Our scripts inherit from Monobehavior
00:02:39
so that we have access to these features.
00:02:42
The inheritance structure is hierarchical.
00:02:45
A common way to think of inheritance
00:02:46
is to think of the animal kingdom.
00:02:48
In this example, we'd have a
parent class called animal.
00:02:52
This class would contain
all of the definitions
00:02:54
and properties necessary to make
00:02:55
the class behave like an animal.
00:02:58
From this animal base class, we might
00:03:00
have a couple of child classes,
vertebrate and invertebrate.
00:03:04
The vertebrate class would then, in turn,
00:03:06
be the parent class for more classes
00:03:08
such as mammal, reptile, or amphibian.
00:03:11
Each of these child classes would take
00:03:13
the information given by its base class
00:03:15
and add to it.
00:03:16
Just like our animal example, inheritance
00:03:18
in object oriented programming
00:03:20
is known as an IS-A relationship.
00:03:23
This means that the child
class is a parent class.
00:03:26
A reptile is a vertebrate.
00:03:29
A mammal is an animal.
00:03:31
An example in Unity that you
may have come across before
00:03:34
is a Capsule Collider is a Collider.
00:03:38
This concept will be covered further
00:03:39
in the lesson on
polymorphism linked below.
00:03:43
The idea of inheritance can be very useful
00:03:45
and applicable in game development.
00:03:47
We might, for example, have
a class called humanoid.
00:03:51
This class covers all of the things
00:03:52
that humanoids should do in our game.
00:03:54
We then have two child
classes, enemy and player.
00:03:58
These control the
specifics have how players
00:04:00
and enemies work in the
game while already behaving
00:04:03
like humanoids, because they've inherited
00:04:05
all of the humanoid class's members.
00:04:07
We could then have two more child classes
00:04:09
of enemy, orc and goblin.
00:04:12
These already behave like enemies
00:04:14
which in turn behave like humanoids.
00:04:16
In this way, we have
much less code to write
00:04:18
to make orcs and goblins behave
00:04:20
as we want them to, because we're reusing
00:04:22
the code from humanoid and enemy.
00:04:25
Constructors are an exception
00:04:26
to what is inherited by child classes,
00:04:28
as they remain unique to a class
00:04:30
and are never shared.
00:04:32
When a constructor is called
in a child class, however,
00:04:34
the constructor of its parent class
00:04:36
is called immediately before.
00:04:38
Since classes can have many
different constructors,
00:04:40
we might want to be able to control
00:04:42
which base class
constructor is being called.
00:04:44
We do this with the keyword base.
00:04:47
By following the parameter list
00:04:49
of the child's constructor with a colon,
00:04:51
you can explicitly call
a specific constructor
00:04:53
of the base class using the keyword base
00:04:56
in the base constructor's parameter list.
00:04:58
If the base class's constructor
is not called explicitly,
00:05:02
then the default constructor will still
00:05:04
be called implicitly.
00:05:08
Aside from calling the
base class's constructor,
00:05:10
you can also use the base keyword
00:05:12
to access other members of the base class.
00:05:14
This is useful for situations where
00:05:16
you wish to access the base
class's version of something,
00:05:18
because it is different
than the derived version.
00:05:21
This happens often when
overwriting functions.
00:05:23
For more information on this,
00:05:25
see the lesson on overriding linked below.
Inheritance
5 Mins 32 Secs
Fruit Class
using UnityEngine;
using System.Collections;
//This is the base class which is//also known as the Parent class.publicclassFruit
{publicstring color;
//This is the first constructor for the Fruit class//and is not inherited by any derived classes.publicFruit(){
color = "orange";
Debug.Log("1st Fruit Constructor Called");
}
//This is the second constructor for the Fruit class//and is not inherited by any derived classes.publicFruit(string newColor){
color = newColor;
Debug.Log("2nd Fruit Constructor Called");
}
publicvoidChop(){
Debug.Log("The " + color + " fruit has been chopped.");
}
publicvoidSayHello(){
Debug.Log("Hello, I am a fruit.");
}
}
Apple Class
using UnityEngine;
using System.Collections;
//This is the derived class whis is//also know as the Child class.publicclassApple : Fruit
{
//This is the first constructor for the Apple class.//It calls the parent constructor immediately, even//before it runs.publicApple()
{
//Notice how Apple has access to the public variable//color, which is a part of the parent Fruit class.
color = "red";
Debug.Log("1st Apple Constructor Called");
}
//This is the second constructor for the Apple class.//It specifies which parent constructor will be called//using the "base" keyword.publicApple(string newColor) : base(newColor)
{
//Notice how this constructor doesn't set the color//since the base constructor sets the color that//is passed as an argument.
Debug.Log("2nd Apple Constructor Called");
}
}
FruitSalad Class
using UnityEngine;
using System.Collections;
publicclassFruitSalad : MonoBehaviour
{
voidStart(){
//Let's illustrate inheritance with the //default constructors.
Debug.Log("Creating the fruit");
Fruit myFruit = new Fruit();
Debug.Log("Creating the apple");
Apple myApple = new Apple();
//Call the methods of the Fruit class.
myFruit.SayHello();
myFruit.Chop();
//Call the methods of the Apple class.//Notice how class Apple has access to all//of the public methods of class Fruit.
myApple.SayHello();
myApple.Chop();
//Now let's illustrate inheritance with the //constructors that read in a string.
Debug.Log("Creating the fruit");
myFruit = new Fruit("yellow");
Debug.Log("Creating the apple");
myApple = new Apple("green");
//Call the methods of the Fruit class.
myFruit.SayHello();
myFruit.Chop();
//Call the methods of the Apple class.//Notice how class Apple has access to all//of the public methods of class Fruit.
myApple.SayHello();
myApple.Chop();
}
}