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
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
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
Child classes can inherit from a parent class.
In this lesson, two classes will inherit from a parent class.
First, open up the folder for this lesson by following this path in your course resources.
And now from the requirements, we can identify what seems to be three objects, store shirt and pants.
We're not going to criticize or object and we're not going to be writing any unit tests just to keep
the focus on inheritance.
OK, let's get right to it, so first we have to create a shirt class.
The requirements tell us that shirt has Fairfield's private string size.
Private double price.
Private string color.
And private string brend.
And naturally, we would create a constructor that receives four parameters.
OK, now you want to set every field in the object that's being created equal to a parameter this size,
equal size, the stock price equals price, the stock color.
Equals color.
This thought brand equals brand.
For now, don't do anything else for this class.
We'll move on and create a pince class.
Public class pant's.
And instead of a size field, the pants class defines a waste field.
The remaining fields are identical to the ones for shirt, so could we just copy them over?
You know how I feel about copying and pasting code.
You should never do it before.
Now I'll entertain it.
Let's go through with it.
And I guess we could also copy and paste the constructor.
Change shirt, the pants, and instead of shrink size put into waist.
Now, on that very same note, never, ever copy and paste code.
If you copy and paste code, that means you're doing something wrong.
What we did here is really, really bad.
If you decide to copy and paste now, then you would end up copying and pasting together as the setters.
Let me generate them really quick.
And then, heck, why not copy and paste those as well?
OK, now I hope you see my point, never copy and paste code, it's really, really bad practice.
This is why inheritance is so important.
Inheritance makes code reuseable shirt and pants need to inherit from a parent class.
Shirt and pants share similar fields.
Instead of defining the same fields in each class, they should inherit common fields from a parent
class.
In other words, shirt and pants are children of the product class inside models create a product class
product Java.
The product class is going to define fields that shirt and pants have in common.
It's a private double price.
Privates Tranquila.
Private string brand.
And now you can remove these fields from Pentz.
We'll get errors here, so remove the field updates inside the constructor and delete all the letters
and setters.
We'll do the same thing for shirt.
Remove the field updates in the constructor.
As well as the getters and setters.
Now, a class that extends another class inherits from its.
Pants and shirt must extend the product class.
And that's really it now the pants class and shirt class are inheriting from their parent class product.
OK, now how can we make sure that pants and shirt are actually inheriting from the product class easy
inside main an object of the shirt class.
Percent a size of small.
A price of five ninety nine.
A color of blue.
And a brand of will say job over time.
Now, put a break point.
And run the debugger.
It creates a new object of the class, you can see the object includes the field inside the child.
Class size.
But wait a second, it also includes the fields that an inherited price color and bred.
Let's make sure Pence is inheriting from product create a new object of the pants class.
Passing a waste of 30 to a price of twenty four point ninety nine, a color of blue.
And a brand of, let's say, Javor Klein.
Run the debugger.
It creates a new object of the pants class.
You can see that the object includes the field inside the child class waste.
But wait, it also includes the fields that an inherited price, color and bread, that is inheritance.
All I want you to take away from this lesson is this when two classes share common fields, never defined
the same fields in each class.
Instead, each class should inherit common fields from a parent class.
Child classes can inherit from a parent class.
When two classes share common fields, never defined the same fields in each class.
Instead, they should inherit common fields from a parent class.
Shirt and pants shared similar fields, but instead of defining the same fields in each class, they
inherited common fields from a parent class.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.