‫So in the previous sections we made heavy use of inheritance through our actors.
‫Now what is inheritance?
‫Inheritance is when a child class automatically has all of the functionality of its parent.
‫We say essentially that the child is a parent.
‫So where did we use that previously?
‫Well, for example, in our moving platforms, we had an actor that was a moving platform actor, and
‫then we had different children of that actor that gave us the spinning, rotating platforms that up
‫and down platforms, the side to side pushers.
‫Those were all children.
‫They all were movers.
‫You could say the moving pusher is a mover.
‫We could say that the rotating baton is a mover.
‫So there is an alternative to using inheritance, which is a bit all or nothing.
‫And this is composition which is a bit more pick and choose.
‫You get to say that Class A has an instance of Class B, it can choose to use this functionality, but
‫it doesn't have to.
‫It's not tied in to being class B, it can just choose to take the bits it wants.
‫So we say that Class A has a class B, so let's have a look at some examples of inheritance versus composition
‫using something quite natural.
‫I think talking about inheritance in the animal kingdom, we might have a class an actor say that is
‫an animal actor and it has a method on it, which is to live its lives.
‫When you call that method now, it makes sense to have things like a mammal that inherits from this
‫because it also needs to live.
‫So it needs all of that functionality that the animal has, but it adds some more functionality, such
‫as making milk.
‫We might also have a reptile that has the functionality, the added functionality of laying eggs.
‫Now, it's fair to say that a mammal is an animal and a reptile is an animal as well.
‫Now we could go onwards and say, okay, we've got a cow.
‫Well, a cow clearly is a mammal.
‫It needs to make milk.
‫And by extension, it also needs to live because it's also an animal.
‫A snake is a reptile and it is an animal, but it is not a mammal.
‫So that makes sense, too.
‫But what about a platypus?
‫A platypus suddenly needs to both be a mammal and a reptile because it needs the functionality of laying
‫eggs and it needs the functionality of making milk and it needs to be an animal.
‫So it gets a little bit confusing here with inheritance.
‫And inheritance can put us in a straightjacket and make these situations really hard to resolve, which
‫is why composition is what comes to the rescue.
‫Here is the has a relationship.
‫So instead of trying to build this taxonomy of classes with animals and mammals, etc., what we do
‫instead is we say we've got a milk making component and that component has the functionality of making
‫milk.
‫So the cow would have one of these milk making components and it gets its functionality.
‫From there, the snake can have its egg laying component and it gets its egg laying functionality from
‫there.
‫And the platypus can have both components.
‫It doesn't have to pick and choose one component.
‫It can have all of them, and it gets to share the functionality just fine.
‫So let's take the example that we're going to be using here, where we've got a mover, we've got something
‫that wants to move.
‫We might have started setting it up as an inheritance and is a relationship.
‫So we've got a mover that inherits from this static mesh actor, kind of like we've done in previous
‫sections, and then from the mover, we might inherit a moving door or a moving wall.
‫But what happens if we wanted to do a moving light?
‫How could that be done?
‫We can't inherit from mover because that would make it a static mesh actor and we want to use a light
‫actor instead as the parent for moving light.
‫How could we resolve this problem?
‫Using has a relationships instead pause a video, have a think about it, get out a piece of paper and
‫jot down some of your thoughts.
‫Okay.
‫Welcome back.
‫So this is the way I would resolve the problem so we could have our moving door and moving well.
‫Both inherit from static.
‫Actor That's fine.
‫And the moving light can inherit from the light.
‫Actor But now the difference is that we include this has a relationship in here as well.
‫So the moving door, the moving wall and the moving light all can have a mover component on the actor.
‫And this is where actor components come in really handy.
‫And we're going to put this into practice in the next lecture, so I'll see you there.