Java for Fucking Idiots: Learn the Basics of Java Programming Without any experience!


new Goblin(); But we can throw a massive curveball here. See how the data type of the goblin variable is Goblin



Download 3,91 Mb.
Pdf ko'rish
bet69/70
Sana02.04.2022
Hajmi3,91 Mb.
#524999
1   ...   62   63   64   65   66   67   68   69   70
Bog'liq
Java for Fucking Idiots

new
Goblin();
But we can throw a massive curveball here. See how the data type of the
goblin
variable is Goblin ? And the constructor we’re using is 
Goblin()
?
Since a Goblin is a Monster, we can instead do this:
Monster warts = 
new
Goblin ();
Whooooaaaa! They don’t have to be the same? Nope!
The data type on the left does not have to be the same as the constructor
you call on the right. The one on the right can be a child’s constructor.
Why polymorphism?
What does this get us? It’s a bit like abstraction; we can abstract away what
is behind the mask.
Remember the fight method on Monster?
public void
attack(Monster monsterToAttack) {
monsterToAttack.
health
-=
damage
;
}
There’s a Monster being passed to this method. Is it a Goblin? Golem?
Zombie? Some other kind of Monster?
We don’t need to give a single shit. We can treat them all like Monsters;
Java will call the appropriate methods on the children without us needing to
care.
Can you imagine how annoying it would be if there were different types of
Lists, Monsters, Heroes, Screens, Coffee, etc. and you had to be able to
account for all of them individually?
Java uses polymorphism to allow us to code against the parent data type of
a class, while it takes care of dynamically running the methods of the child
implementation of that class.


Splash
class
Magikarp
extends
Monster {
Magikarp() {
damage
= 5000
0
;
}
void
attack(Monster monsterToAttack) {
System.
out
.println(
"Magikarp used SPLASH! But nothing happened!"
);
}
}
Check that out. It’s a new kind of monster that does something totally
different when it attacks; nothing!
Now, what happens if we run this code?
Monster fishy =
new
Magikarp();
fishy.attack(goblin);
Do we fail to compile?
Do we splash and do 0 damage?
Do we attack and deal 50000 damage?
Does Java call the “attack” method on the parent class Monster,
or the “attack” method on the child class Magikarp?
My head. My poor, poor head.
The answer is we splash around and nothing happens, of course! Note that
the data type of fishy is Monster . But we created a 
new Magikarp()
.
Again, this ability for Java to call the child class’s method, when the
variable is created using the data type of the parent, is called polymorphism
.
Real examples of polymorphism?
One incredibly common use of polymorphism in Java is Lists . I briefly
mentioned these in the array section…to say I wouldn’t be covering them.


But for this section, I will if only for a moment.
List
firstList =

Download 3,91 Mb.

Do'stlaringiz bilan baham:
1   ...   62   63   64   65   66   67   68   69   70




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish