Object-Oriented Programming
Get an overview of object-oriented programming and classes in JavaScript.
Introduction to object-oriented programming
Object-oriented programming or OOP is a style of programming that encapsulates related pieces of code in objects that maintain state throughout the life of the program. The objects can then be reused or easily modified as required. Many modern languages such as Java, C++, Ruby and Python are object oriented.
Three key concepts in OOP are:
Encapsulation
Polymorphism
Inheritance
Note: There is a fourth key concept, “Abstraction,” which is not covered in this course. Abstraction in OOP refers to simplifying complex systems by focusing on essential properties and behaviors while hiding irrelevant details, allowing for a high-level and generalized representation of objects and their interactions. It involves creating clear interfaces and encapsulating implementation details.
We’re going to use the example of a coffee machine to illustrate how each of these concepts can be applied in a programming environment. In many ways, a coffee machine can be thought of as an object, because it has properties such as speed, strength, and capacity, and it also has methods or actions it can perform, such as brewing, switching on, and switching off.
Encapsulation
When the coffee machine is turned on, it makes some noises and then, a few minutes later, it produces a steaming cup of hot coffee. We don’t need to know how the machine works in order to make the perfect cup of coffee; we just press on. This demonstrates the concept of encapsulation: the inner workings are kept hidden inside the object and only the essential functionalities are exposed to the end user, such as the on button. In OOP, this involves keeping all the programming logic inside an object and making methods available to implement the functionality, without the outside world needing to know how it’s done.
Get hands-on with 1400+ tech skills courses.