We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 10
a @pyplane. code
Javascript
OOPre ) @pyplane_code O1
Introduction
In this tutorial we will explore the fundamentals
for Object Oriented Programming (OOP) in
Javascript in a practical way with examples
Table of contents
¢ first class
¢ first instances
¢ inheritance
¢ encapsulation
¢ polymorphism
,° abstract classesé ) @pyplane_code o2
First class
On this slide we are creating our first class.
Remember to add the constructor method that
will enable the creation of instances (objects)
based on our class
class Animal {
constructor(name, age) {
faecal eT
Dp CmE-\+( Ea
bd
(otra mene
iad
“The name of the animal is
${this.name} and age is ${this.age}*
)
i6 ) @pyplane_code ‘o}c}
First instances
On the previous slide we created anew class, addeda
constructor method that takes in 2 arguments - name
and age. To create an object from this constructor
method we need to use the new keyword and pass in
the given name and age of the animal object that we
are creating. We also have added a getInfo method
that returns the information about the object
eo tam eee te et LOM VP TG > ae)
console. log(firstAnimal)
console. log(firstAnimal.getInfo())
const secondAnimal = new Animal('Barney', 5)
console. log(secondAnimal)
console. log(secondAnimal.getInfo())
PU Me Hoe Ce
ee Ra
a TM EMT SD
The name of the animal is Barney and age is 5(6 ) @pyplane_code (ey!
nati t-te
C9 TS Mantel c= pre Ve L aU Mgr] -U-)>) (Xe | ga =X OR oh 1 Cy
all the methods and properties of another one (parent class) and
makes it possible to extend the parent class by adding more
BCS a cou iane
Coe aac eT a se
Breau)
Bere a)
o
Tren
cea
H
+
eee lacie iet
Cees SCL nS
Brean uae
Pent aes st
ba
rey an acre eeU )
console. Log(myDog. get Info())
UCR Nr a)
ee Tem Ciao}
Ca eee Gee Tce
console. log(myCat.getInfo())
ComeMel oien sta)
i ee a eee ed
Ce)
Cres
The name of the animal is Whiskers and age is 5
ca@ ) @pyplane_code @) 5
Encapsulation
Encapsulation is arestrictionmechanism making accessing
the data impossible without using special methods
dedicated for this. In the example below we marked weight
as a private property and in order to get and set a value we
need to use the getter and setter method
Germ Tat cue Ur Lane
#weight; ——»_ mark as private
constructor(name, age, weight) {
super(name, age)
this.#weight = weight
}
PiaasCO mee ———— getter
fete gee Sete
}
setWeight(weight) {——»__.- sett ter
eC em hd
V
¥
Output:
eee Nec amet meh Ol Fel te PT
console. log(myCat.getWeight()) 5kg
myCat. setWeight('6kg') ie
eee eben lads Kel OD)é ) @pyplane_code 06
Polymorphism
Polymorphism is a concept that utilizes inheritance for reusing
methods multiple times with a different behaviour depending on class
types. To understand this lets look at our example - in the dog class
we will remove the bark method andin the animal class we'lladda
makeSound method which will be overridden by cat and dog classes
ere Urea class Cat extends Animal {
Erasce tr ae ont Coa Le msc a ne
Cee eat Era CS)
ES ey Pret aene tg
}
}
cco
a makeSound() {
Eee oT
i] +
+ 2
}
COSC ean (ae a
Cae ae a Oe 1 ee oe PL
ECD Roc ear an
Seatac a ee
Ee
ECCT OPEL LCs OD)
Ree ec)
CIC Cera Lora)
makeSound() {
oa
}
i output:
woof
meowee rey,
Abstract class p1
Abstract class is a class which can't be instantiated and
require subclasses which inherit from a particular
abstract class to provide implementations. We will
change the Animal class to an abstract class. It will not
be possible to create ainstance of this class anymore
like on slide number 3 and we will mark makeSound as an
abstract method -in order to use it, asubclass must
declare its own implementation of this method
class Animal {
aaa eT oe 8
ea
this.age = age;
pRICPeR edie UPC LO ne
throw new Error("Can't create a instance of Abstract class");
}
b
LLC LC LS
SNe ee Coe eet SC
esé ) @pyplane_code os
Abstract class p2
EER fmre uC UF L Mee
fe aaa acl Pee |+ Pe a De
ETI)
BP aa gl
LLL O mane
cad
class Cat extends Animal {
fl aaa ate G LL eee|: eee
Tea T+ 0
this.weight = weight
TES LO
ih CA Led
THE 3s
Y can't
// const myAnimal = new Animal('Barney', 3) ——» instantiate
const myDog = new Dog('Rex', 2, ‘German Sh.') anymore
const myCat = new Cat('Whiskers', 5, '5kg')
this will give us an error since
CLE Meche LC Oa cactoltiatel Maite elel ane cel eel aDid you learn something?
Don't forget to save this post and drop alike
Follow me for much more stuff like this :)
wu
a soli
ole clot ed
Lv | Like Save for later i