Encapsulation: ============== -> Fundamental concept of OOPs. -> Encapsulation is process of bundling/binding of data (attributes) and methods which operate on the data into the single unit or class. -> this can restrict direct access to some of an object's data/components which can prevent the accidental modification. ex: class A{ data1; data2; m1(){ } m2(){ } } class B{ } class Person{ #name; // private members #age; constructor(name,age){ this.#name = name; this.#age = age; } getName(){ return this.#name; } setName(name){ this.#name = name; } getAge(){ return this.#age; } setAge(age){ if(age > 0){ this.#age = age; } else{ console.log("Age must be above zero."); } } } let p1 = new Person("Kiran",33); console.log(p1.getName()); console.log(p1.getAge()); p1.setName("Kishore"); p1.setAge(32); console.log(p1.getName()); console.log(p1.getAge()); p1.setAge(-10); ==================================================== Polymorphism: ============= Poly + morphs ==> Many Forms -> Polymorphism can be implemented in to ways: 1) Overloading 2) Overriding class A{ m1(x, y){ implementation; } } method overloading: describes that: the method signature can be change with the same implementation. Method Overriding: describes that: the method signature can fix but the implementation can be differ. m1(x ,y){ --- ---- ===== } -> Overloading is possible within the same class and also possible in outside the class. -> Overriding is always possible in outside the class only. -> For implementing the overriding of method, we need the inheritance. Inheritance: ============ Acquiring of properties of one class into another class. we can create a new class from another class. Here: new class ==> Child class another class ==> Parent class -> Types of inheritances: 1) Single Inheritance 2) Multiple Inheritance ==> Not supported 3) Multi-level Inheritance 4) Hierarchy Inheritance 5) Hybrid Inheritance ==> Not supported. class Animal{ constructor(name){ this.name = name; } speak(){ console.log(`${this.name} makes a sound.`); } makeSound(sound = "generic sound"){ console.log(`${this.name} makes a ${sound}`); } makeSound(){ console.log("...."); } } class Dog extends Animal{ constructor(name,breed){ super(name); this.breed = breed; } // Overridng the speak() from parent class in child class. speak(){ console.log(`${this.name} barks.`); } //Overload makeSound() from Parent class in child class. makeSound(sound = "bark"){ console.log(`${this.name} makes a ${sound}`); } } let d = new Dog("Buldo","Golden Retriever"); d.speak(); d.makeSound(); let p = new Animal("cat"); p.speak(); p.makeSound(); p.makeSound() Multi-level Inheritance: ======================== class GrandParent{ m1(){ console.log("Hi"); } m2(){ console.log("Hello"); } } class Parent extends GrandParent{ m3(){ console.log("Good Morning."); } } class Child extends Parent{ m4(){ console.log("Good Evening."); } } let c = new Child(); c.m4(); c.m3(); c.m2(); c.m1(); =================================================== Hierarchy Inheritance: ====================== class A{ addition(a,b){ return a+b; } } class B extends A{ subtraction(a,b){ return a-b; } } class C extends A{ multiplication(a,b){ return a*b; } } cobj = new C(); console.log(cobj.multiplication(7,9)); // console.log(cobj.subtraction(97,79)); console.log(cobj.addition(97,79));