Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Russian
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
0 Now that we've already talked about abstraction and encapsulation and seen it in action, let's move on 1
to the next pillar of OOP which is inheritance. And inheritance, 2
we're talking about it in programming refers to when a class is able to inherit properties and methods 3
from their parents. 4
So you for example might have inherited your eye color from your parents or you might have inherited 5
certain behaviors like you are a neat freak and you really really need to keep everything super tidy. 6
You might have gotten that from your mom or your dad. 7
Similarly all classes can inherit from a superclass or a parent class and it can inherit properties 8
as well as methods. 9
And this is important because when we're abstracting our program and we're creating lots of different 10
modules lots of different components that all take care of a specific task. 11
It's likely that we will have some duplication in their functionality. 12
So for example our pastry chef and our chef will probably have some similar functionality. 13
They probably both know how to boil some water. 14
They both probably have a method for baking something but they might do it slightly differently. 15
So in order to prevent having to rewrite lots of code when we're abstracting into lots of different 16
classes and separate modules, this is why inheritance is really key when you're creating objects. To demo 17
this, 18
I'm gonna go into a empty DartPad and I'm going to create a new class. 19
Let's say that I create a Car class and my Car class is going to have some properties. 20
Every car probably starts out with a number of seats property right? 21
And we're gonna set it to 5 as the default. 22
Every car I build is going to start out with 5 seats. 23
Seems pretty standard. 24
And then every car is also going to have a drive method because otherwise, why have a car 25
If it doesn't? And when you call the drive method, it simply just turns the wheels. 26
So I'm going to create a print statement that says the 'wheels turn'. And now when I create my car object 27
from my Car class, 28
so let's create a new car. Let's call it myNormalCar because a little bit later on, we're going to obviously 29
make this a lot more souped up. So myNormalCar is just going to be created from the bog standard car 30
class. 31
So when I print myNormalCar 32
.numberOfSeats for example, then you can see that I get 5. And when I say myNormalCar. 33
drive, then it will try and perform the drive functionality and the wheels start turning. 34
That's all pretty standard and we're familiar with how classes objects work in this way now. 35
But what if I wanted to create a different type of car? 36
What if I wanted to create an electric car? 37
Well an electric car is kind of similar to a car right? 38
It probably also has five seats and it probably can drive as well, but it might have some other functionality. 39
Let's go ahead and create a new class and this class is going to be our ElectricCar. 40
But I don't really want to have to type out all of this again. 41
It's just the same as a normal car. 42
But I want to focus my time on explaining how it's different from a normal car. 43
So how can I inherit all of the things I get in car? 44
Well in Dart, the keyword is extends. 45
So my electric car is going to extend my Car class. 46
And what this means is that my ElectricCarr by default, is going to have everything that a car has. 47
So I could go ahead and create a new electric car. 48
Let's call it myTesla and let's create it as a new electric car. And now I can straight up just say 49
myTesla.drive and you'll see that it'll make the wheels turn. 50
Let's go ahead and comment out the previous ones that we had related to our car so we're not getting 51
confused in the console, and you can see this myTesla drive triggers that electric car to turn its wheels 52
because it inherited all of that capability from its parent class, the Car class, and it also has a starting 53
default number of seats. 54
But by inheriting from the Car class, it means I don't have to repeat myself and type out everything 55
that is common to cause an electric cars and I can just focus my time on creating code that is different. 56
For example my electric car might have a battery level and it's going to start out at 100%. But 57
when it goes down, I can recharge my electric car so I can have a method called recharge. 58
And this will reset the battery level to 100% 59
so now not only does my Tesla drive, it also can recharge 60
and I didn't have to repeat the things that a car already has. 61
I've managed to incorporate the properties and methods simply by using that extend keyword. 62
This is how inheritance works and we can see this in our code as well. 63
Every single time that we've created a new stateful widget or stateless widget, we haven't have to 64
create all the properties or the functionality of a stateless or stateful widget. 65
We've simply been able to tap into it by creating a class that extends the parent class a stateful 66
or stateless widget. 67
And if you hold down COMMAND or CONTROL on Windows and you click on that class, you can see that it has 68
various properties and methods that we've been able to simply use because we're extending it. We're 69
inheriting from it and all we have to do is to describe all the things are different from the parent 70
class and focus on building out our custom functionality rather than on the basic boring things such 71
as how do we draw a widget on screen? 72
How do we give it a different color? 73
How do we change its shape and size? 74
How do we actually move the pixels around? 75
That's all sorted out for you by the Flutter SDK and all of that capability are packaged into these 76
parent widgets that we're able to inherit from. Now in the next lesson, 77
I want to talk a little bit about the last pillar which is polymorphism. 78
So for all of that and more, I'll see you on the next lesson.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.