Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
46 views

? Object-Oriented Programming (OOP) in JavaScript!

Loren

Uploaded by

20bcsme037kaleem
Copyright
© © All Rights Reserved
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
0% found this document useful (0 votes)
46 views

? Object-Oriented Programming (OOP) in JavaScript!

Loren

Uploaded by

20bcsme037kaleem
Copyright
© © All Rights Reserved
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. eo) t Cin = m4 F = By =< a What is OOPs Object-oriented programming (OOP) is a programming paradigm that is based on the concept of “objects”. * which can contain data (in the form of “properties’) and code (in the form of "methods’). + OOP is a popular programming paradigm because it allows for modular, reusable code that can be easier to read, maintain, and scale. + There are two types of OOP languages: + Class-Based languages like JAVA, C++. + Prototype-Based languages like JS. 2 Features Of OOPs There are four rules or main pillars of Object- oriented programming language. This defines how the data and actions associated with the data; are organized using code. + OOPs Concepts * Objects + Classes * inheritance * polymorphism * encapsulation * abstraction * In a previous post we discussed JavaScript classes, objects and properties. E} Inheritance Inheritance is a concept where one class (child class) inherits properties and methods from another class (parent class). * In JavaScript, inheritance is achieved through the use of the extends keyword. class Animal { eta aie OCCT S Pecan ree ear , eons console.log(*${this.name} is eating.°); , H Ce ie aurea Peaag ace Cr ee Ce cone Broa Gru ae this.breed = breed; } bark() { CeCe Oc re nu Se CH LSU. Bee Coins ark nae area ec myDog.eat(); // 0 in Det era Ora T ope k ry Polymorphism Polymorphism is the ability of objects to use the same function in different forms. * This reduces repetition and makes the code snippet useful in many different cases. * In JavaScript, polymorphism is achieved through method overriding or method overloading. * Method overriding is where a_ subclass provides its own implementation of a method that is already defined in the parent foi[o [io * Method overloading is where a class has multiple methods with the same name but different parameters. LY Here's an example of polymorphism in JavaScript using method overriding Cee at Create Grote mee this.color = color; i draw() { CeCe Great imu } } Cremona Cu CRCI Tan draw() { console.1og( "Drawing a ${this.color} circle.*); i 5 See eC Mace Sie Cae draw() { Cee eer RMT cre sme } } Cuno tem mrss Cnet io Fy Sa rata a cae Le Dette i 0 pamela ir - Dene LETT OIF yet Drawi eae a Here both override the draw() method of the parent class, but provide their own implementation of it. 6 Encapsulation Encapsulation is the practice of hiding the internal details of an object from the outside world. Crem yatta PCreu Nea em ens ee constructor(initialBalance) { this.#balance = initialBalance; 2 getBalance() { Pere seit u t SG COO Se BEL Gc console. log(myWallet..getBalance()); // output: 100 + By encapsulating the #balance field within the Wallet class, we are preventing direct access to the #balance field from outside of the class. * This is an example of how encapsulation can help to prevent unwanted modifications in a real-world scenario such as managing a wallet. 7 Abstraction Abstraction is the process of hiding the implementation details while showing only the necessary information to the user. Erm cia constructor (amount) { Pree eeee Cia bi CAO mnt throw new Error("pay() method must be implemented") ; 3 iY class StripePayment extends Payment { See CU eaccie i ree } PAO me COSC aa cuca Ta mec Eee } } OCR Gace ae ae Potiacia cata ie | reese } pay() { nes Sos ae Ss es NEES Fa 2 } Cea Cem te Poe Ey 1O rH nen) 7 Cone ete ae) iene Pea Ten RUC ECR } + In this example, the Payment class is the abstract class that defines the interface for making payments. * It has a pay method that is abstract and must be implemented by its subclasses. * The StripePayment and PaypalPayment classes are concrete classes that implement the pay method for their respective payment gateways. + As always, | hope you enjoyed the post and learned something new. * If you have any queries then let me know in the comment box. bE) i post about Tech, Coding and Career Follow for more content like this

You might also like