Object Oriented Programming Basic Terminology and concepts, an easy to understand with example guide!

Welcome to the world of Object-Oriented Programming! If you are new to OOP world, you may feel overwhelmed by the complexity of OOP concepts and terminologies, but I’m here and try to make it easy for you.

In this article, I’ll guide you through the basics of OOP, breaking down the key concepts and providing real-world examples to help you understand the power of this programming paradigm.

So, please grab a cup of coffee, get comfortable, and let’s dive in! Together, we’ll unlock the secrets of OOP and take your coding skills to the next level.

 

Let’s start with some terminology:

Object Oriented Programming is about organizing code around Objects. So what is an Object?

An object is an instance of a class; in other words, an object is a specific realization of a class with its unique attributes and behaviors.

For example, if you have a class “Person”, you can create multiple objects from that class, each representing a different person (like Amin, John, or even Samantha), with their specific name, age, address, etc.

Objects interact with each other to accomplish tasks and solve problems, making OOP a powerful tool for creating complex and scalable software systems. Simply put, an object is a single, identifiable unit in a program that has both data and behavior.

As we have read, we see objects are a realization of classes; so what is a class?

A class is a blueprint or a template for creating objects (instances of the class). It defines the attributes and behaviors that the objects of the class will have.

For example, consider the class “Person” which we mention. This class might have attributes such as name, age, address, and behaviors such as eating, sleep, and work. When you create an object (Amin, John, or Samantha) of the class “Person”, you can set its attributes to specific values and use its behaviors to perform actions.

Two more terms that can cause ambiguity are object state and behavior. Let’s see what these two terms mean:

Object state also referred to as object’s properties, instance variables, member variables, instance fields, object data, or simply state, refers to the set of values that define the properties or characteristics of an object at any given time.

The object’s state is stored in its variables and is unique to each object. Let’s get back to our “Person” class; each object created from the “Person” class will have its unique state, determined by its specific values set for “name” and “age” at any given time.

On the other hand, object behaviors are the actions that an object can perform or the methods that an object can execute. An object’s class defines its behavior, specifying the methods the object can perform. For example, in our “Person” class with methods “eat()” and “sleep()”, each object created from the “Person” class will be able to perform these actions. The object’s behavior is a crucial aspect of its implementation and is essential to defining the object’s interface to the rest of the system.

In simpler words, an object’s behavior is its methods, and its state is the values of its variables or properties. Usually, people from different backgrounds (pascal, Fortran, java, etc.) use different terminology to refer to these terms.

Object Oriented Programming, as I said, everything is organized around objects; now, we know what an object is. On the other hand, OOP is based on four powerful concepts, which are: Inheritance, Polymorphism, Abstraction, and Encapsulation. Let me explain them one by one and provide some easy-to-understand examples:

 

What is Inheritance?

Inheritance is a mechanism where an object (child) inherits the properties (Variables) and behaviors (Methods) of another object (parent). Think of it as a child inheriting traits from their parents.

For example, consider a “Person” class with properties like name, age, and address. A “Student” class can inherit the properties of a “Person”, and add additional properties such as a student ID and major. The “Student” class is the child, and the “Person” class is the parent. This way, you don’t have to define all the properties of a “Person” in the “Student” class again. The “Student” class inherits all the properties of the “Person” class, so you can reuse the code and keep your program organized.

Let’s see a pseudo code for this example:

As you can see in this pseudo code, the “Student” class, inherited from the “Person” class, has all its properties and Methods without defining them again. Now let’s take a look at encapsulation.

 

What is encapsulation?

Encapsulation is a concept where the internal details of an object are hidden from the outside world and can only be accessed through a public interface. Think of it as a person’s personal life that is not accessible to everyone.

For example, consider a “Person” class with properties like name, age, and address. These properties can be considered private and can only be accessed through public methods like “getName()” and “setName()” (you have to ask for them, and they are not visible to you).

This way, you can control how the properties are accessed and modified (you can limit the access and modification of these properties) and ensure that they are only changed with the right intent and in a valid state. Encapsulation helps to keep the internal workings of an object hidden and makes it easier to change the implementation without affecting the rest of the code.

Let’s see a pseudo code for that:

As you can see in this pseudo code, you can control how the “Person” class properties are accessed and modified. You can keep an object’s internal workings hidden and make it easier to change the implementation without affecting the rest of the code. Now let’s take a look at Polymorphism.

 

What is Polymorphism?

Polymorphism is a concept where an object can take many forms. It allows objects of different classes to be treated as objects of the same class. Think of it as a family member who can take on different roles and responsibilities. In Polymorphism the subclass may accept the implementation of a parent class behavior without overriding it or it may override it completely to produce completely different results or functionality. Let’s see how:

Consider a “Family Member” class with a method called “role”. A “Mother” class and a “Father” class can both inherit from the “Family Member” class and override the “role” method to define their specific role in the family. When you call the “role” method on a “Mother” object, it returns “Mother”, and when you call the “role” method on a “Father” object, it returns “Father”.

This way, even though the “Mother” and “Father” classes have different roles, they can still be treated as objects of the same class, and you can write code that can handle them in the same way.

Let’s see a pseudo code for it:

As you see in this code, I demonstrate Polymorphism by using inheritance to create two classes, “Father” and “Mother”, that inherit the “Parent” class. The “Parent” class has a method parent_behavior() that implements the standard behavior of all parents. However, the “Father” and “Mother” classes each have their implementation of parent_behavior() that is specific to fathers and mothers, respectively. When these classes are instantiated, the same method name parent_behavior(), is used to produce different behaviors for the various objects, showing the polymorphic nature of the classes.

Now let’s take a look at the final concept, which is abstraction.

 

What is Abstraction?

Abstraction is a concept where you focus on the essentials and ignore the details. It allows you to work with objects at a higher level of abstraction without worrying about the implementation details. Think of it as using a phone without knowing how it works.

Consider a “Phone” class with methods like “call()” and “text()”. When you use these methods, you don’t need to know how they are implemented; you know that they allow you to make a call or send a text. This way, you can write code that uses the phone without knowing how it works internally. Abstraction makes it easier to change the implementation of a class without affecting the rest of the code because the code that uses the class only interacts with its public interface. Let’s see a pseudo code for it:

In this code, I demonstrate abstraction by using inheritance to create two classes, “Smartphone” and “FeaturePhone”, that inherit the “Mobile” class.

The “Mobile” class has two abstract methods, make_call() and send_message(), that are left to be implemented by the subclasses.

The “Smartphone” and “FeaturePhone” classes implement these methods to meet their specific requirements.

When these classes are instantiated, the same method names, make_call() and send_message() are used to produce different behaviors for the various objects, showing the abstract nature of the “Mobile” class.

 

Here we come to an end in describing the concepts. As you may see, I used two more terms: instantiated and public interface. Let me clarify those two:

A public interface refers to the set of actions, behaviors, or methods that an object can perform and are available to other objects in the system. Simply put, it is the public face of an object, what the object can do, and how different objects in the program can use it. In other words, it defines how other objects can interact with the object in a clear and well-defined manner. The public interface typically consists of public methods and properties (variables) that are part of the object’s class definition (you read implementation).

Instantiation, on the other hand, is the creation of an object from its class. And that’s all to it.

 

The harsh truth and the reality:

All the concepts I have outlined above were available before the OOP paradigm was created! But with OOP, the implementation gets easier and fits real-world problems better. And that’s all to it.

 

Final thoughts:

As a software engineer or a newbie in the programming world, you understand the importance of structuring and organizing code. By utilizing the concepts and principles of OOP, you can write more maintainable, reusable, and easy-to-understand code.

Object Oriented Programming is like the Pacific Ocean. Mastering it needs continuous learning and practice, and what I have written above was just a scratch of its surface.

Remember that these four concepts are the most powerful assets you have in your OOP adventure.

Comments (0)
Leave your comment