Principles of object-oriented programming

Tutorial

·

foundational

·

+10XP

·

10 mins

·

(202)

Unity Technologies

Principles of object-oriented programming

In this tutorial you’ll learn about the basics of object-oriented programming paradigm and its four associated principles.

By the end of this tutorial, you will be able to:

  • Define encapsulation
  • Define inheritance
  • Define polymorphism
  • Define abstraction
  • Explain how the pillars of OOP work together to create organized, efficient code

Languages available:

1. Overview

As you gain more practice with programming, you’ll eventually begin to identify situations where you suspect code could be written more effectively. Perhaps you’ve found yourself copying and pasting lines of code, or changing protection levels on variables multiple times as you add more scripts to a project. Regardless of the specific situation, these observations are an important part of becoming a better programmer — you’re thinking beyond pure code functionality, and considering your code’s usability.


There are many ways to improve the usability of code, and even many different opinions about the best approach! This can be overwhelming for newer programmers. The best way to gain experience is to try these approaches yourself. To help you get started, we’ll show you one of the most popular programming paradigms (or patterns): object-oriented programming.


2. What is object-oriented programming?

Object-oriented programming (OOP), is a programming pattern in which the methods and variables that are related to one another are grouped together to form what is called an object (the “object” in OOP!).



Keeping data organized into objects has many advantages. It makes code more comprehensible, so that it is easier to understand what the code is trying to accomplish. In the example above, we can see that our Rabbit object has a few variables to describe its physical appearance, and a method that defines how it can move. It would be confusing if you opened up a separate class named Burrow and discovered a set of Rabbit related methods for eating carrots and nose wiggling.


Objects also allow you to control your code’s accessibility, which means that you’ll reduce the chance of someone else (or even yourself!) accidentally breaking your code as you create your projects. You’ll also be able to more easily reuse and repurpose different parts of your code.


Keep in mind that every project has at least two programmers working on it: you, and you six months from now, when you won’t remember how or why you did what you have done. Object-oriented programming will make your code more readable and difficult to break for your own sake, as well as for any other programmer who might work on your project.


There are four main principles of object-oriented programming, which are commonly referred to as the pillars of OOP. They are: abstraction, encapsulation, inheritance, and polymorphism. To successfully use the object-oriented programming pattern, you need only learn and apply these four principles! We’ll learn about each one of these pillars in detail later on, but for now, let’s review them at a high level.


3. The Four Pillars


Abstraction is the process of removing complex code from the scripts where other programmers will see it, and only exposing the functionality other programmers really need. When you “abstract out” the details, you reduce duplicate code and provide easy access to the most useful methods. The goal of abstraction is to keep your code as clean as possible, and simple for other programmers (or yourself!) to use. In our Rabbit example above, the Hop method would be a good example of abstraction. Without the Hop method, if a programmer wanted the Rabbit to move, they would have to write code that would access its RigidBody and set its velocity, hop height, and so on. With the Hop method, all of those aspects are abstracted away, allowing the programmer to focus on when the Rabbit should hop rather than how.


Inheritance is the process of creating a primary class from which other classes, referred to as child classes, can be created. A child class takes on all of the features of the primary, or parent class, automatically. This reduces the need to rewrite code that both classes would need to make use of. As an example, let’s say that we wanted to create a new class called Bunny. Like the Rabbit class, it has ears that can be floppy or standing, has fur that can be a specific color, and can hop. Without inheritance, you’d essentially have to copy all of the code you already wrote and paste it into the new class. With inheritance, you simply extend the Rabbit class, and that functionality is already there and accessible for the Bunny class. The Bunny class could then go on to feature Bunny-specific functionality, such as eating Bunny chow.


Polymorphism is one of the most useful aspects of using inheritance. It allows you to create alternative functionality for code that's been inherited from a parent class. As an example, our child Bunny class represents a domesticated Rabbit. It’s able to hop just as a Rabbit is able to, but a Bunny should probably be a little bit slower than its wild parent counterpart. With polymorphism, you can override the contents of the Hop method and write custom code that’s unique to the Bunny. The method call remains the same, but the correct code will be called based on which entity it was called on.


Encapsulation is similar to abstraction in that its overarching purpose is to separate the programmer from code complexity, but the focus here is more on code safety in the form of accessibility. Encapsulation gives you tools to code for other programmers, and make sure that they only use your variables and methods as intended. In encapsulated code, other programmers can’t easily change the values of variables or the properties of objects. It’s impossible to account for all of the different ways that other scripts might access your code, so it's far better to encapsulate what you’ve created so it can only perform as intended. As an example, let’s say that our Rabbit’s ear type affects its hearing ability. Once that value is set, it shouldn’t be changed later on. To ensure that the value is protected, you would set it as private, preventing any outside scripts from accessing it.


4. Object-oriented programming in the missions

If you feel as if some of this is sounding familiar to you, you’re right! You’ve actually already been implementing some OOP principles into your code throughout this pathway. As you work through the rest of this mission, try to think about what pillar you might be applying as you write new functionality for your project. In the next mission, we’ll revisit each pillar in depth as you look for ways to iterate and improve on your code.


5. Next Steps

It’s important to note that object-oriented programming isn’t the only programming paradigm out there. There are many different approaches for creating applications, and odds are good you won’t use the object-oriented approach for everything you make in Unity. Object-oriented programming, and in fact all programming patterns, are just tools in your toolbelt, and while some may be more effective at some things than others, there is no one true superior paradigm. Always remember to select the tool that makes the most sense for the challenge you’re facing. That said, object-oriented programming is well established and a great tool for new programmers. As you move forward through these next missions, you’ll expand your knowledge on these core concepts.


In the next tutorial, it’s time to get started with the project! You’ll implement the scene flow and configure buttons so that the user can move between the two scenes and exit the application.


Complete this tutorial