Last active
August 22, 2019 03:51
-
-
Save ShilpiMaurya/d1b957973d61afbc733e6e8fe5313bf8 to your computer and use it in GitHub Desktop.
object oriented programming in js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Try edit message | |
// Object oriented prograaming: 4 principles | |
// 1. Encapsulation | |
// 2. Inheritance | |
// 3. Composition | |
// 4. Polymorphism | |
//encapsulation | |
class Rectangle { | |
constructor(length, width){ | |
this.length = length; | |
this.width = width; | |
} | |
printArea(){ | |
console.log('area is: ', this.length * this.width); | |
}apis | |
printParameter(){ | |
console.log('parameter is: ', 2(this.length + this.width)); | |
} | |
} | |
// inheritance | |
class Square extends Rectangle{ | |
constructor(side){ | |
super(side, side); | |
} | |
} | |
class NewSquare{ | |
constructor(side){ | |
//composition | |
this.myRectangle = new Rectangle(side, side); | |
} | |
printArea(){ | |
this.myRectangle.printArea(); | |
} | |
printParameter(){ | |
this.myRectangle.printParameter(); | |
} | |
} | |
// consumer | |
const myRectangle = new Rectangle(2, 3); | |
myRectangle.printArea(); | |
const mySquare = new Square(2); | |
mySquare.printArea(); | |
const myNewSquare = new NewSquare(2); | |
myNewSquare.printArea(); | |
// polymorphism | |
function add(arg1, arg2){ | |
return arg1 + arg2; | |
} | |
console.log(add(1,2)); | |
console.log(add("1","2")); | |
console.log(add("abc","def")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment