Object-oriented programming (OOP) is a fundamental programming paradigm used by nearly every developer at some point in their career. OOP is the most popular programming paradigm used for software development and is taught as the standard way to code for most of a programmer’s educational career. Another popular programming paradigm is functional programming, but we won’t get into that right now.
Today we will break down the basics of what makes a program object-oriented so that you can start to utilize this paradigm in your algorithms, projects, and interviews.
Now, let’s dive into these OOP concepts and tutorials!
Object-Oriented Programming (OOP) is a programming paradigm in computer science that relies on the concept of classes and objects. It is used to structure a software program into simple, reusable pieces of code blueprints (usually called classes), which are used to create individual instances of objects. There are many object-oriented programming languages, including JavaScript, C++, Java, and Python.
OOP languages are not necessarily restricted to the object-oriented programming paradigm. Some languages, such as JavaScript, Python, and PHP, all allow for both procedural and object-oriented programming styles.
A class is an abstract blueprint that creates more specific, concrete objects. Classes often represent broad categories, like Car
or Dog
that share attributes. These classes define what attributes an instance of this type will have, like color
, but not the value of those attributes for a specific object.
Classes can also contain functions called methods that are available only to objects of that type. These functions are defined within the class and perform some action helpful to that specific object type.
For example, our
Car
class may have arepaint
method that changes thecolor
attribute of our car. This function is only helpful to objects of typeCar
, so we declare it within theCar
class, thus making it a method.
Class templates are used as a blueprint to create individual objects. These represent specific examples of the abstract class, like myCar
or goldenRetriever
. Each object can have unique values to the properties defined in the class.
For example, say we created a class,
Car
, to contain all the properties a car must have,color
,brand
, andmodel
. We then create an instance of aCar
type object,myCar
to represent my specific car.We could then set the value of the properties defined in the class to describe my car without affecting other objects or the class template.
We can then reuse this class to represent any number of cars.
Object-oriented programming (OOP) has been around for decades. As the original object-oriented language, Java is a mainstay in the world of computer programming. Having a foundation in OOP Java concepts will allow you to write cleaner, more modular, and more reusable code, as well as make it easier for you to understand the codebases of different companies you might be interested in joining. Starting with the basics and reviewing complex topics like inheritance and polymorphism, this course is filled with illustrations, exercises, quizzes, and hands-on challenges. You’ll walk away with an understanding of classes and objects behavior and be able to easily create simple, efficient, reusable and secure code.
Let’s take a real-world problem and conceptually design an OOP software program.
Imagine running a dog-sitting camp with hundreds of pets where you keep track of the names, ages, and days attended for each pet.
How would you design simple, reusable software to model the dogs?
With hundreds of dogs, it would be inefficient to write unique entries for each dog because you would be writing a lot of redundant code. Below we see what that might look like with objects rufus
and fluffy
.
//Object of one individual dogvar rufus = {name: "Rufus",birthday: "2/1/2017",age: function() {return Date.now() - this.birthday;},attendance: 0}//Object of second individual dogvar fluffy = {name: "Fluffy",birthday: "1/12/2019",age: function() {return Date.now() - this.birthday;},attendance: 0}
As you can see above, there is a lot of duplicated code between both objects. The age()
function appears in each object. Since we want the same information for each dog, we can use objects and classes instead.
Grouping related information together to form a class structure makes the code shorter and easier to maintain.
In the dogsitting example, here’s how a programmer could think about organizing an OOP:
The diagram below represents how to design an OOP program by grouping the related data and behaviors together to form a simple template and then creating subgroups for specialized data and behavior.
The Dog
class is a generic template containing only the structure of data and behaviors common to all dogs as attributes.
We then create two child classes of Dog
, HerdingDog
and TrackingDog
. These have the inherited behaviors of Dog
(bark()
) but also behavior unique to dogs of that subtype.
Finally, we create objects of the HerdingDog
type to represent the individual dogs Fluffy
and Maisel
.
We can also create objects like Rufus
that fit under the broad class of Dog
but do not fit under either HerdingDog
or TrackingDog
.
Next, we’ll take a deeper look at each of the fundamental building blocks of an OOP program used above:
In a nutshell, classes are essentially user-defined data types. Classes are where we create a blueprint for the structure of methods and attributes. Individual objects are instantiated from this blueprint.
Classes contain fields for attributes and methods for behaviors. In our Dog
class example, attributes include name
& birthday
, while methods include bark()
and updateAttendance()
.
Here’s a code snippet demonstrating how to program a Dog
class using the JavaScript language.
class Dog {constructor(name, birthday) {this.name = name;this.birthday = birthday;}//Declare private variables_attendance = 0;getAge() {//Getterreturn this.calcAge();}calcAge() {//calculate age using today's date and birthdayreturn Date.now() - this.birthday;}bark() {return console.log("Woof!");}updateAttendance() {//add a day to the dog's attendance days at the petsittersthis._attendance++;}}
Remember, the class is a template for modeling a dog, and an object is instantiated from the class representing an individual real-world item.
Enjoying the article? Scroll down to sign up for our free, bi-monthly newsletter.
Objects are, unsurprisingly, a huge part of OOP! Objects are instances of a class created with specific data. For example, in the code snippet below, Rufus
is an instance of the Dog
class.
class Dog {constructor(name, birthday) {this.name = name;this.birthday = birthday;}//Declare private variables_attendance = 0;getAge() {//Getterreturn this.calcAge();}calcAge() {//calculate age using today's date and birthdayreturn Date.now() - this.birthday;}bark() {return console.log("Woof!");}updateAttendance() {//add a day to the dog's attendance days at the petsittersthis._attendance++;}}//instantiate a new object of the Dog class, and individual dog named Rufusconst rufus = new Dog("Rufus", "2/1/2017");
When the new class Dog
is called:
rufus
name
& birthday
arguments, and assigns valuesProgramming vocabulary:
In JavaScript, objects are a type of variable. This may cause confusion because objects can be declared without a class template in JavaScript, as shown at the beginning.
Objects have states and behaviors. The state of an object is defined by data: things like names, birthdates, and other information you’d want to store about a dog. Behaviors are methods the object can undertake.
N/A | What is it? | Information Contained | Actions | Example |
---|---|---|---|---|
Classes | Blueprint | Attributes | Behaviors defined through methods | Dog Template |
Objects | Instance | State, Data | Methods | Rufus, Fluffy |
Attributes are the information that is stored. Attributes are defined in the Class
template. When objects are instantiated, individual objects contain data stored in the Attributes field.
The state of an object is defined by the data in the object’s attributes fields. For example, a puppy and a dog might be treated differently at a pet camp. The birthday could define the state of an object and allow the software to handle dogs of different ages differently.
Methods represent behaviors. Methods perform actions; methods might return information about an object or update an object’s data. The method’s code is defined in the class definition.
When individual objects are instantiated, these objects can call the methods defined in the class. In the code snippet below, the bark
method is defined in the Dog
class, and the bark()
method is called on the Rufus
object.
class Dog {//Declare protected (private) fields_attendance = 0;constructor(name, birthday) {this.namee = name;this.birthday = birthday;}getAge() {//Getterreturn this.calcAge();}calcAge() {//calculate age using today's date and birthdayreturn this.calcAge();}bark() {return console.log("Woof!");}updateAttendance() {//add a day to the dog's attendance days at the petsittersthis._attendance++;}}
Methods often modify, update or delete data. Methods don’t have to update data though. For example, the bark()
method doesn’t update any data because barking doesn’t modify any of the attributes of the Dog
class: name
or birthday
.
The updateAttendance()
method adds a day the Dog
attended the pet-sitting camp. The attendance attribute is important to keep track of for billing Owners at the end of the month.
Methods are how programmers promote reusability and keep functionality encapsulated inside an object. This reusability is a great benefit when debugging. If there’s an error, there’s only one place to find it and fix it instead of many.
The underscore in _attendance
denotes that the variable is protected and shouldn’t be modified directly. The updateAttendance()
method changes _attendance
.
Learn to write cleaner, more modular, and more scaleable code in Python by gaining a master of Object Oriented Programming (OOP). You'll start with the basics of object oriented programming and build up to more advanced concepts such as inheritance, information hiding, and polymorphism. Along the way you'll learn how each concept applies to Python in particular, as well as how various Python features make it particularly convenient for OOP. This course is filled with hands-on coding challenges, playgrounds, snippets, and illustrations to keep things interactive.
The four pillars of object-oriented programming are:
Inheritance: child classes inherit data and behaviors from the parent class
Encapsulation: containing information in an object, exposing only selected information
Abstraction: only exposing high-level public methods for accessing an object
Polymorphism: many methods can do the same task