Career Development

What Are the Four Basics of Object-Oriented Programming?

November 23, 2020

Knowledge of computer programming is in high demand in today's technology-driven society. Having knowledge of object-oriented programming (OOP) can be valuable when developing and maintaining software programs. In this article, we discuss the basic principles of OOP and explain them in easy-to-understand examples.

Related: 15 Computer Science Jobs That Pay Well

What is object-oriented programming?

Object-oriented programming combines a group of variables (properties) and functions (methods) into a unit called an "object." These objects are organized into classes where individual objects can be grouped together. OOP can help you consider objects in a program's code and the different actions that could happen in relation to the objects.

This programming style widely exists in commonly used programming languages like Java, C++ and PHP. These languages help simplify the structure and organization of software programs. Programmers often use OOP when they need to create complex programs.

Related: Technical Resume Writing: Tips and Examples

Easily apply to jobs with an Indeed Resume

What are the four basics of object-oriented programming?

Object-oriented programming has four basic concepts: encapsulation, abstraction, inheritance and polymorphism. Even if these concepts seem incredibly complex, understanding the general framework of how they work will help you understand the basics of a computer program. Here are the four basic theories and what they entail:

  • Encapsulation
  • Abstraction
  • Inheritance
  • Polymorphism

Encapsulation

The different objects inside of each program will try to communicate with each other automatically. If a programmer wants to stop objects from interacting with each other, they need to be encapsulated in individual classes. Through the process of encapsulation, classes cannot change or interact with the specific variables and functions of an object.

Just like a pill "encapsulates" or contains the medication inside of its coating, the principle of encapsulation works in a digital way to form a protective barrier around the information that separates it from the rest of the code. Programmers can replicate this object throughout different parts of the program or other programs.

Abstraction

Abstraction is like an extension of encapsulation because it hides certain properties and methods from the outside code to make the interface of the objects simpler. Programmers use abstraction for several beneficial reasons. Overall, abstraction helps isolate the impact of changes made to the code so that if something goes wrong, the change will only affect the variables shown and not the outside code.

Inheritance

Using this concept, programmers can extend the functionality of the code's existing classes to eliminate repetitive code. For instance, elements of HTML code that include a text box, select field and checkbox have certain properties in common with specific methods.

Instead of redefining the properties and methods for every type of HTML element, you can define them once in a generic object. Naming that object something like "HTMLElement" will cause other objects to inherit its properties and methods so you can reduce unnecessary code.

The main object is the superclass and all objects that follow it are subclasses. Subclasses can have separate elements while adding what they need from the superclass.

Polymorphism

This technique meaning "many forms or shapes" allows programmers to render multiple HTML elements depending on the type of object. This concept allows programmers to redefine the way something works by changing how it is done or by changing the parts in which it is done. Terms of polymorphism are called overriding and overloading.

Related: Computer Skills: Definitions and Examples

Object-oriented programming examples

Using real-world examples of OOP helps simplify these complex concepts for greater understanding. Here are a few ways to visualize the principles behind OOP:

  • Example of encapsulation
  • Example of abstraction
  • Example of inheritance
  • Example of polymorphism

Example of encapsulation

Beginning programmers may better understand this concept in relation to how a browser functions. Browsers have local storage objects that allow you to store data locally. These objects have properties, like length, which turns the number of objects into storage, along with methods like (remove item) and (set item).

Another way to consider encapsulation is in terms of an employee's pay. The properties of an employee can include base salary, overtime and rate with a method called factor wage. Code written in an encapsulated, object-oriented way functions with fewer and fewer parameters. The fewer the number of parameters, the easier it is to use and maintain that function.

Example of abstraction

Think of a stereo system as an object with a complex logic board on the inside. It has buttons on the outside to allow for interaction with the object. When you press any of the buttons, you're not thinking about what happens on the inside because you can't see it. Even though you can't see the logic board completing functions as a result of pressing a button, it's still performing actions. This is an abstraction, which can be used to understand the concept of programming.

Another way to understand this is to consider the human body. The skin acts as an abstraction to hide the internal body parts responsible for bodily functions like digesting and walking.

Example of inheritance

Consider two classes. One is the superclass (parent) while the subclass (child) will inherit the properties of the parent class and modify its behavior. Programmers applying the technique of inheritance arrange these classes into a hierarchy of "is-a-type-of" relationships.

For instance, in the animal world, an insect would be a superclass. All insects share similar properties, such as having six legs and an exoskeleton. Grasshoppers and ants are both insects and inherited similar properties.

Example of polymorphism

To better understand the two terms of polymorphism called overloading and overriding, it helps to visualize the process of walking. Babies learn to crawl first by using their arms and legs. Once they learn to stand and walk, they are ultimately changing the body part used to accomplish the act of walking. This term of polymorphism is called overloading.

To understand the next term of overriding, think of how you naturally walk in the direction you are facing. When you stop and walk backward, this changes the direction of your path and also the mechanism of function. You are overriding the natural action that you usually complete.