OOP

Ian Acosta
Aug 26, 2017 · 3 min read

OOP

What is OOP? OOP, which stands for Object-oriented programming is a concept that allows you to efficiently create, store, and organize data. As the name suggests, object-oriented programming involves the use of objects. OOP acts like a blueprint because it allows you to create multiple objects(houses) with the same properties(structure).

https://udemy-images.udemy.com/course/750x422/636248_1cfd_2.jpg

Classes and Class Instances

A class is a variance of OOP because it uses the same concept of making multiple copies of something whilst maintaining the same properties. A class contains a function called a constructor, which lets you declare the properties you want your class to have. For example:

class Book{
constructor(title, author, pages){
this.title = title;
this.author = author;
this.pages = pages;
}
}

The class Book above uses the constructor function to add the properties title, author and pages.

let bigBook = new Book('The Fault in Our Stars', 'John Green', 317);
let smallBook = new Book('The Cat in the Hat','Dr. Seuss',79);

After you set those properties, you are now allowed to make multiple instances of that class. Although bigBook and smallBook were all made from one class, they all contain their own unique assets because they were declared with different values. Using classes make it easier to create these instances because you wouldn’t have to retype the same thing multiple times. Instead you can just use one line of code to make these copies.

Classes have getter and setter methods that lets you change the value and receive information from declared instances of a class.

class Book{
...
get bookInfo(){
return `Your book is ${this.title} by ${this.author} with a total of ${this.pages} pages`;
}
set pages(pages){
this.pages=pages;
}
}
bigBook.bookInfo;
bigBook.pages = 113;

As you can see, the getter and setter methods are denoted with the terms get and set. Inside the get method, it contains a return message that will be sent back when it’s called. Likewise, the set method will change the page value when it has been re-declared like a variable. This will change bigBook’s pages value from 317 to 113.

Class Inheritance

Class inheritance is basically a way to make sub-classes of an existing class while carrying on traits owned by the parent class. This concept allows you to create a more specific class of a class. For example:

class Textbook extends Book{
constructor(title, author, pages, subject){
super(title, author, pages)
this.subject = subject;
}
}

This code shows the creation of a new class called Textbook. You can tell that it’s a sub-class of Book because the term extends follows the new class name that’s being declared. For Textbook to inherit the properties(title, author, pages) from Book, it needs to call the parent class by using the method super. This eliminates the process of having to set those properties again.

In addition to class inheritance, sub-classes such as Textbook can add more properties in the constructor. This will not affect or change anything in the Book class, but just the sub-class alone. These new properties give the sub-class an reason for existence because without it, instances made from Textbook will just have the same properties as Book.

)
Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade