if (Model.NotificationsEnabled) { }
Maker.io main logo

What is Object-Oriented Programming?

2025-01-13 | By Don Wilcher

micro:bit

Object-oriented programming, or OOP, is a powerful coding approach that can significantly enhance software and embedded project development for makers and hobbyists. By organizing code into reusable and modular components, OOP allows for an effective way to build embedded projects. This tutorial introduces you to OOP principles using JavaScript on the BBC Micro:bit. The BBC Micro:bit is a popular embedded platform for creative electronics projects. We'll explore the fundamental principles of OOP and demonstrate how they can be applied to real-world projects. By the end of this tutorial, you'll have a solid understanding of how to use OOP in your embedded projects.

What is Object-Oriented Programming? OOP for the BBC Micro:bit.

OOP Basics

The object-oriented programming approach is based on the concept of objects. An object is a software entity that contains data and programming code, and the data is built using fields. Fields are the characteristics or properties that describe the object. The programming code is structured as a procedure, which can also be called the “method.” The method establishes the behavior or action of the object.

To categorize the data and programming code that can be identified in OOP, the word class is used. A class in OOP is a template that defines the object’s characteristics and methods of the specific software entity. Another way of visualizing class is as a blueprint that defines the structure and behavior of objects. Building new characteristics or behaviors of an existing object is to create an instance or modified copy of the software entity. Instances allow you to reuse the same class to create multiple modified copies of the original object. With instances, the data and behavior will be aligned with the modified object. Therefore, a class encapsulates data and behavior within an object using OOP.

What is Object-Oriented Programming? Creating an Instance of an LED in JavaScript.

Four Core Principles of OOP

The framework that establishes OOP is built on four programming principles: encapsulation, inheritance, polymorphism, and abstraction. These programming approaches work together to build modular, reusable, and easily manageable code. Understanding these four OOP principles builds sophisticated and flexible embedded projects.

Encapsulation

The bundling of data and methods that operate within the class is encapsulation. The internal details of the class's internal workings are hidden while encapsulated. With such an approach, the integrity of the object’s internal states is protected. Only the necessary parts of code that interact with the embedded hardware, like general-purpose input-output (GPIO) pin-connected devices, are not encapsulated.

What is Object-Oriented Programming? The variables x and y are protected from outside interaction using encapsulation.

Inheritance

Inheritance allows you to create new classes based on existing ones. To achieve this coding task of creating new classes, a child class or subclass needs to be established. The child class will receive the same properties and methods as an existing class known as the parent class or base class. Another way to view inheritance is like a family tree where the grandparent’s wealth is distributed among the siblings. The wealth in the case of an LED is the creation of various blink rates. The advantage of this OOP approach is that code reuse is promoted, and creating classes is easy and manageable. A Blinking LED that obtains characteristics of LED, and a blink method is an example of inheritance. The overall result is a blinking LED.

What is Object-Oriented Programming? The JavaScript blinking LED class is created through inheritance.

Polymorphism

Objects that are allowed to be treated as a class object or instances of their parent class rather than their actual class is called polymorphism. Polymorphism is a Greek word that means poly(many) and morph (form), meaning many forms. Polymorphism using JavaScript is illustrated by creating a parent class LED and extending it to two different child classes with unique behaviors. The extension to different classes with unique behaviors demonstrates the many forms of instances that can be developed from the parent object. Therefore, many methods of operating an LED can be created using polymorphism. The benefits of OOP polymorphism are different classes can be called using the same programming interfaces, thereby allowing flexibility and reusability in maker or hobbyist-embedded project code.

What is Object-Oriented Programming? The LED blink and fade behaviors are created by Polymorphism.

Abstraction

Abstraction is an OOP principle that focuses on revealing the important details of an object while hiding the internal working complexities of the software entity. Abstraction simplifies the use of objects by providing a clear mechanism for interacting with them using methods. An understanding of the underlying process is not required for interaction. When trying to understand the process from a coding perspective, OOP abstracts the circuit’s complexity using a method like on() or off(). Another perspective on abstraction is to view the OOP principle as an approach to focusing on the coding rather than the hardware complexities.

What is Object-Oriented Programming? The abstraction of the class LED ‘on’ and ‘off’ method.

Example of a Class and an Object in OOP

To aid in understanding classes and objects, the following JavaScript code will illustrate the coding structure using the OOP approach. The initial class keyword is used to define classes. The class keyword is followed by the class name. The constructor is established by the _init_method. In Javascript, the _init_method is abstracted away. With this constructor, the object’s properties and initial values are set. Therefore, the constructor’s purpose is to initialize programming variables built as an object’s property. Establishing this method allows for the use of variables to be used within a target programming language.

What is Object-Oriented Programming? JavaScript class and object for a BBC micro:bit LED.

Conclusion

The object-oriented programming approach offers a way to structure your embedded project code professionally and efficiently. With the use of modularity, reusability, and ease of management, code like JavaScript, Python, and C++ can be organized effectively. The four core principles of OOP, consisting of encapsulation, inheritance, polymorphism, and abstraction, will ensure the embedded project code is modular and reusable. In addition, using classes and objects aids in creating embedded project code that is easy to manage and share, among others. Happy Coding!

制造商零件编号 MICRO:BIT SINGLE
BBC MICRO-BIT V2 SBC BOARD ONLY
Micro:bit
Add all DigiKey Parts to Cart
Have questions or comments? Continue the conversation on TechForum, DigiKey's online community and technical resource.
mmmmmmmmmmllimmmmmmmmmmlli