What is Object-oriented Programming?

Object-oriented programming explained

Object-oriented programming is a programming paradigm that uses Objects to create applications. These objects can have data about a certain aspect of the program in the form of properties, and methods that can perform actions.

Let's take the example of a Bank.

A bank would have the class 'Bank Account' which would include properties like 'Account Holder Name' and 'Account Number'. It would also have methods to perform actions like depositing and withdrawing money.

A class is simply just an instance of an object which provides a blueprint for what the class should consist of, such as properties and methods.

What are Properties and Methods in an Object?

Properties

Properties are basically values that are associated with an object.

A 'Person' object could have the property name 'hair_colour' and 'name'. The values associated with these property names could be 'Blonde' and 'Louie'.

Here's an example in JavaScript which will make this more clear.

Object showing a property example

This object called 'person' has 2 properties as mentioned before.

You can now access these properties using the dot notation.

console.log(person.name);
// Returns "Louie"

Methods

Methods are very similar to functions which are built in to an object.

We could add on to our first example and add a method into the object.

Object showing a method example

We have added a method called "sayName" which when called would look something like this.

person.sayName();
// Returns "Hi, my name is Louie!" in the console.

You may of spotted the keyword "this" when access the name property in the method.

What is "this"?

"this" is what we use to access properties inside an object.

"this" is referring to the object itself. It wouldn't understand what value you are trying to access if you just put "name" and not "this.name".

I will write a blog post discussing "this" in more detail since it can be quite a hard concept to grasp.

The 4 Concepts of Object-oriented Programming

Abstraction

This is when you are being provided with only the essential information. This means you can work out from a high-level perspective what each class does. For example a Deck of Cards class might have a shuffle() and deal() method.

Encapsulation

Encapsulation is when an object keeps its state private. The idea is that the properties and methods are owned by the class and other that classes cannot access its data.

Inheritance

Inheritance is when a class inherits properties and methods from a parent class.

An example would be an Animal class having the methods of 'eat()' and 'sleep()' and then a new class called Dog which inherits the Animal class because they share properties in common.

Polymorphism

This has the most complex name and probably the hardest to grasp. Polymorphism means it has many forms and shapes.

This links to inheritance because if you inherit methods from a parent class, you want the child class to use these methods to suit their own context.

Such as creating an Animal class and each child class like "Dog" and "Cat" will have their own sound if you called the "playSound" method.

Here's an article from freeCodeCamp that explains 4 concepts in a basic manner that is easy to understand.

Object-oriented Programming Languages

A couple popular programming languages which use the Object-oriented Programming (OOP) paradigm are Java and C#.

Java

Java appeared in 1995 that was designed to allow programmers to write once and run the application anywhere. If your device supports Java then you can run any Java application on it.

Java was created by Sun Microsystems but was acquired by Oracle in 2010.

C#

C#, pronounced (see sharp) was developed in around 2000 by Microsoft. C# is a multi-paradigm programming language that is a strongly typed language.

If you have ever programmed in JavaScript, you may of heard of TypeScript which was also developed by Microsoft to bring more of a strict syntactical superset of Javascript which allows for static types.

These types provide a better way to describe the shape of an object which provides for better documentation and allowing TypeScript to validate your code is working properly. TypeScript is 100% JavaScript, so whatever works in JavaScript will also work in TypeScript.

Join the mailing list!