Published
Edited
Insert cell
# Object Oriented Programming
Insert cell
md`---

## Creating a New Class`
Insert cell
class avenger {

#HeadQuartersPassword = "4V3NG3R5";
constructor(name, age, power) {
this.name = name;
this.age = age;
this.power = power;
this.rescue = () => `${this.name} used ${this.power} and rescued a civilian!`;
this.unlockDoor = () => {
const chars = this.#HeadQuartersPassword.length;
return `${this.name} unlocked the door by entering password: ${"*".repeat(chars)}`
}
}
}
Insert cell
md`----

## Creating Objects from Classes`
Insert cell
ironMan = new avenger("Iron Man", 42, "an iron suit");
Insert cell
spiderMan = new avenger("Spider Man", 19, "webs");
Insert cell
scarletWitch = new avenger("Scarlet Witch", 29, "magic")
Insert cell
md`---

## Encapsulation Private Field Example

We can easily access Spiderman's age, but we cannot access the password for the headquarters. We can tell Spiderman to use his password to unlock the headquarters door, but we cannot directly access the password because it is private.`
Insert cell
spiderMan.age
Insert cell
spiderMan.#HeadQuartersPassword
Insert cell
spiderMan.unlockDoor()
Insert cell
md`---

## Abstraction Examples
Object-oriented programming simplifies functionality to methods that take in as few arguments as possible, so programmers don't have to think what arguments a method needs.`
Insert cell
md`### Example 1
With the *calcArea* function below, the programmer has to manually input the height and width arguments.`
Insert cell
calcArea = (width, height) => width * height
Insert cell
calcArea(10, 6)
Insert cell
md`With the object, the programmer only needs to call the method *getArea* to get the area.`
Insert cell
myRectangle = ({
height: 5,
width: 8,
getArea: function() {
return this.height * this.width
}
})
Insert cell
myRectangle.getArea()
Insert cell
md`### Example 2
In this example with Spider-Man, we don't need to know the information of how the hero saves people. We just tell him to go rescue.`
Insert cell
spiderMan.rescue();
Insert cell
md`---

## Child Class Example`
Insert cell
class retiredAvenger extends avenger {

rescue = () => `${this.name} is retired. Call an active Avenger for help.`;
}
Insert cell
md`---

## Inheritance Example
*retiredAvenger* is a child class of *avenger*, so it has inherited the attributes and methods of the parent class.
When we create a new object *captainAmerica*, it is a within the *retiredAvenger* class, he can still unlock the HQ door with his password due to the method inherited from the parent class *avenger*.`
Insert cell
captainAmerica = new retiredAvenger ("Captain America", 180, "a shield")
Insert cell
captainAmerica.unlockDoor();
Insert cell
md`---

## Polymorphism Example
When defining the class *retiredAvenger* the *rescue()* method was replaced.

The *rescue()* method will behave differently for a *retiredAvenger* and an object from the parent class *Avenger*`
Insert cell
captainAmerica.rescue();
Insert cell
spiderMan.rescue();
Insert cell
md`---

---`
Insert cell
md`## Helpful Links

- https://www.educative.io/blog/object-oriented-programming

- https://www.youtube.com/watch?v=pTB0EiLXUC8`
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more