All language subtitles for pragstudio-ruby-20-inheritance (Transcribed on 24-Apr-2023 21-23-11)

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified) Download
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

Now in this section we want to talk about inheritance and we're referring to inheritance

as a software design concept.

It's taken from the real world.

So you inherit certain characteristics from your parents but you also have unique qualities

of your own.

In the same way in software we can model an inheritance relationship between classes.

So we might have some child classes that inherit behavior from parent classes but they also

might have their own unique behavior.

So a good example of this is like a 3D movie.

It has similar characteristics to a regular movie but it also has unique qualities.

You also have to wear special glasses at a 3D movie.

That's true.

Now to create a 3D movie we don't want to copy all the code that we already have in

a movie class.

Rather we just want to extend the functionality that we already have.

So we've got the code in one place.

So let's have a look at that.

So we have our movie class here and it has all the basic behavior of a movie but we want

a 3D movie.

So I'm going to create a new file.

I'm going to call the file movie3d.rb because class names in Ruby can't start with a number

so I'm going to call it movie3d and then we'll just require our movie because we're going

to base our movie3d class on it.

I'm going to say class movie3d like that and then to inherit from movie we just use the

less than sign and we give it the class name movie.

So that's all we need to do to set up an inheritance relationship between these two classes.

Then I can just go ahead and create a 3D movie movie3d call new.

We can pass in the same attributes here or set up the state the same way we do a regular

movie.

I'm going to call this movie Glee because Glee is a 3D movie and it's going to have

a rank of five and then I can just turn around and print out movie3d has a title and movie3d

has a rank.

So if we run that we get Glee in five.

So we see that we've inherited the attributes of our parent class movie.

We didn't have to do anything special in this class.

We can also turn around and call movie3d.thumbsup for example and then we'll just print out

its rank again.

In fact we'll print out the whole movie3d and if we run that so we start off with five

we get thumbs up to six.

We print out the rank again and notice that the 2S method is being called on the movie

as well.

So it's acting just like a regular movie.

So I want to quickly show you how you can use reflection to look at the class hierarchy

involved here.

Out on the command line I'm just going to load up IRB and then I'm actually going to

I can load that file movie3d.rb.

So this is just going to load in the file that we just created which will define our

movie3d class and you see that we get the output because we've got that little example

code at the bottom.

And then we can look at movie3d what is your superclass?

Well its superclass or its parent is the movie class.

Another one we can try is movie3d.ask it for its entire ancestor chain by calling the

ancestors method.

So we've got movie3d.

Its direct parent is movie.

Movie's parent is object which is a class built into Ruby.

Kernel is actually something that gets mixed in so we can ignore this now.

Also make more sense in the next section and then at the very top is this class called

basic object.

Well let's look at this visually.

If we ask our movie class for its object ID it will look up the inheritance chain to

object which defines object ID.

Object also has a 2S method which is why every object can call 2S.

Now however in our case we've overridden 2S in our movie class.

So when we call 2S on movie it uses our custom version of 2S.

When we call thumbs up on a movie it invokes that method in the movie class.

Now we introduce our 3D movie class and when we call thumbs up on it Ruby looks up the

inheritance chain until it finds the method under movie.

So here's the take away.

When you call a method on an object Ruby first tries to find that method in the object's

class.

When it isn't found Ruby looks in the object superclass and its superclass and so on all

the way up to the top of the chain.

So we've seen how subclasses inherit behavior and attributes from their parents.

Subclasses can also define their own unique behavior that won't be available in their

parents.

Let's try that.

Back in our movie 3D class here we're going to add some new behavior.

It's just going to be a new method.

Movie 3D movies we're going to have a method called show effect and when we call the show

effect method what we want to do is print out wow.

Actually we want to print out wow a whole bunch of times.

The number of times we're going to do it is going to be defined by this wow factor variable.

We're going to set it to 10 and then we're going to print out wow and then we can actually

multiply a string by a number and it'll print it that many times.

So wow times the wow factor.

So now down in here when we're all done we'll just call movie3D.showEffect and it prints

out wow 10 times.

Wow.

Wow.

Okay, so what if we have a regular movie?

Well if I just have a movie variable I have our old friend Goonies here and I could try

to call movie.showEffect.

What happens here?

Well when we run it undefined method show effect for Goonies because movie the parent

class in this case doesn't have a show effect method.

We can only call that on 3D movies.

No wow for Goonies.

Yeah, a big no wow.

Finally subclasses can specialize behavior by overriding methods that are already defined

in their parent class.

Let's do that here up in our 3D movie we're going to define a thumbs up method which we

know is defined in the movie class because we put it up there and we're going to override

that we're going to define a wow factor local variable in here again and what we're going

to do is we're going to increase our rank by one.

Actually we're going to increase our rank by one times the wow factor.

Right because 3D movies regardless of plot should increase in rank.

Yeah, they're going to go up by tens here right?

So we've overridden that method and then down here I'm going to take off the movie we had

before.

So now when we call this when we call thumbs up we run it.

Glee starts out at five that's the initial rank but then goes up to 15 when we thumbs

up it.

So we've overridden the behavior of the thumbs up method that was defined in our parent class.

Thumbs down however is unchanged.

It's still going to decrement the rank by one.

Also worth noting here is that we can access this instance variable at rank which is defined

up in our movie class.

Classes have access to their parent classes instance variables.

Now let's retrace the method look up here.

When we add the method show effect to our 3D movie and then call it there's no inheritance.

The behavior is defined right in the class.

Now when we define thumbs up in our movie 3D class then the old inheritance falls away

and the new method gets called.

In other words the new method overrides the method in the parent class.

Now I want to show you how we can call methods in the parent class from the child class using

something called super.

Alright back in our movie 3D class here we've got this local variable called wow factor.

We'd like to remove this duplication we really want this to be part of the state of a 3D

movie.

So we know that to do that we're going to have to have an initialize method.

Initialize and we'll pass in the initial wow factor and we want to save that in the instance

variable wow factor like that.

But if we do this we're going to override the initialize that's in our parent class.

So we really need to also take in the title and the rank that a regular movie needs.

Now inside of here we could assign an instance variable title and instance variable rank

but our parent class already does that.

We'd rather just let it set up a regular movie and then we just set up the wow factor.

So the way we can do that is by calling super and then just pass the title and the rank.

This will cause the initialize method to be run in the movie class.

Okay, so then when we create a new movie 3D instead of just passing in the title and the

rank there I can also pass in the wow factor.

Let's say the wow factor of Glee was 10.

So when I call new here what's going to happen is it's going to call initialize in our class

movie 3D passing in the title and the rank.

It's going to let the movie set up the title and the rank but then we're going to set the

wow factor because this is part of our specialized state of a 3D movie.

That seems to be initializing right.

So we can go ahead now and instead of having these local variables we'll just change it

over so we've got this being an instance variable.

This is an instance variable just like that and that works as well.

So what else can we do with super?

Well we've got this thumbs up method right now and what we're doing is we're taking the

wow factor times one and then incrementing the rank.

And in our movie class we already have some default behavior.

We're incrementing the rank by one.

So instead of just completely overriding thumbs up here what we'd rather do is do something

like take the wow factor that number of times we'll just call super.

So what this is going to do is it's going to call the thumbs up method in the movie

class however many times we have as a wow factor.

In this case it's going to call it ten times.

And the reason we do it this way is the logic might change in the movie class and we wouldn't

want to duplicate that change down here.

We just let it do whatever it's going to do.

In this case it's just going to increase the rank by one.

So instead we'll let the parent class handle all the updating of the rank by calling super

one time for each wow factor.

Let's change the wow factor to 20.

Sure.

So it starts at five.

The wow factor is going to be 20 in this case.

We run it.

I know this output is a little confusing.

We have a rank of five but then there's this 20 in here and then 25 after that.

So where's that coming from?

Well if we look down our printout we're printing out the rank but then we're calling thumbs

up and there's a put S before it.

So whatever thumbs up returns is going to get printed.

So let's just go ahead and take that out.

So we're going to start with a rank of five and a wow factor of 20.

Let's run it again.

So we start with five and we end up with 25 because we called the thumbs up method in

the parent class 20 times which gave us 20.

Add the initial rank of five and we end up with 25.

So now that we've set up an inheritance relationship between a 3D movie and a movie we really have

this is a relationship.

A 3D movie is a kind of movie.

So therefore we should be able to use a 3D movie in our program anywhere where we expect

to use a movie.

This is sometimes called polymorphism.

So back over in Flix.rb and we're loading up our movies via this load method we could

also go ahead and add a 3D movie into our playlist.

So first I'm going to make sure I require movie 3D up there and then down here I can

say movie 3D equals movie 3D.new.

We'll use Glee again and it'll start with initial rank of five and it'll have a wow

factor of 20.

And then we can take our playlist and we can add that movie because a 3D movie is a movie

the playlist will just happily play that.

It's got a thumbs up method and a thumbs down method and then if we come back out to the

command line we can just run Ruby Flix.rb and we've got that little printed output that

we have at the bottom of our movie 3D but we see a number of viewings.

We can do that and we see that Glee is in the mix.

It got a thumbs up, it consumed some pretzel carbs and we see that we've got all of our

snacks in there.

So it's playing with all the other movies.

Yay!

And with that you're ready for my favorite exercise.

So in the same way that we just created a 3D movie you're going to create two new player

types.

One's going to be a little clumsy and one's going to be a little crazy.

You'll see what I mean.

And in the next section we'll look at another way to share code across classes called mixins.

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.