Understanding the most important terms of OOP - Part I

  • Object
  • State
  • Method
  • Encapsulation
  • Class
  • Inheritance

Let’s understand each of these terms one at a time and build our knowledge incrementally. Towards the end, we shall see how this knowledge can be translated into usable programming knowledge.

So what is an Object?

Object is any Thing. For e.g., I am “Shankar”. This “Shankar” is an object. You are reading this page and you are also an object. This page that you are reading is also an object. These objects can be defined same as what we call noun in English language:

a word (other than a pronoun) used to identify any of a class of people, places, or things (common noun), or to name a particular one of these (proper noun).

So can we then say Noun Oriented Programming language? No. To say that Objects are just nouns is an incomplete definition. Nouns are references to an object and not objects themselves. This is an important understanding which we will understand as we learn the next two jargons of OOP i.e., state and method.

What is a State?

If we take the example of the man object, it has various parts such as

  • eyes
  • nose
  • ears
  • stomach

Through these parts, we can get some valuable information about the object. It's only these parts that give a shape to the object. It's these parts that help us create a mental model of a man. These parts of the object are called States.

An object can have many states and each state holds a single piece of information about the object as pair of name and value.

state_name = state_value

eyes = closed
stomach = empty
legs = tired

We can have a mental model of the man with the state defined above as:

A man who is probably sleeping with eyes closed, empty stomach and with tired legs.

There are also other states of an object that help you see a man. These states are not physical such as eyes and nose but still help us to get a mental model of the object.

state_name = state_value

age = 90
gender = male
name = Gradgrind
social_security_number = 123409876
title = Dr.
profession = Teacher

A man with the above states is probably giving us a mental picture like this:

Some old man called Dr.Gradgrind aged 90, still working as a teacher and has a social security number 123409876.

What is a Method?

Values of different states of an object are subject to change over time. The state of eyes for our man object may be closed at one point of time and open at another point of time. However, the state doesn't change by any magic, rather it involves an action done by the object. Methods are verbs of an objects.

Our man can sleep changing the state of eye from open to closed and it can awake changing the state back to open. These action that are done by the object are called the methods of object. They are also called as behaviors of object.

Not all actions done by an object need to change its state. For eg., man object can do a lot of introspective thinking about itself like "am I hungry?", "am I sleeping?". Even though the questions are different, there is a common action involved in these questions: thinking. To answer these questions, the object can look into its state and reply yes or no.

man object asking itself: Am I hungry?
man processing: Let me see if my stomach feels empty or full. If empty, yes I am hungry or else not.

man object asking itself: Am I sleeping?
man processing: Let me see if my eyes are closed or open. If closed, I am sleeping or else I am awake.

These acts of thinking inspect the current state of the object but don't change the state.

Let's Encapsulate what we learned so far

We started with an incomplete definition of objects. We called it noun to begin with. Whatever can be defined as a noun is also a object.

Objects have states. Each state is a pair of name and value holding a piece of information about the object and help us see an object as it is. States also help us identify if two objects are of same kind by looking at their states. An object with three eyes is definitely not a man object.

Objects have methods. Methods are actions that an object can do. Methods give life to an object. An object without any method is an inanimate. An object with methods is animate. A piece of stone is an object and can have color state as black but it cannot do anything and hence no methods. A dog object can have a color state as brown and it can do many things (eg., bark) and hence has many methods. Stone is inanimate, dog is animate. Inanimate objects can have state but no methods. Animate objects can have both states and methods.

So an object is a bundle of states and methods. Or in other words states and methods are encapsulated to form an object. In OOP, the term Encapsulation is used to describe the binding of states and methods into a single object. As a non-native English speaker, when I first saw the term encapsulation it sounded alien to me and it was only in an OOP book that I first got introduced to this term and I had a very hard time understanding the same. Then much later I looked up in dictionary what encapsulation means

the action of enclosing something in or as if in a capsule: encapsulation of contaminants within a solid glass-like matrix | encapsulation of the iron prevents its interaction with food.

That definition is something that I can easily manage to digest. Now try to encapsule states and methods to form the object and that is OOP's encapsulation.

Class and Inheritance are covered in (part II)[/oop-terms-II] of this post.

Do you find this post interesting? Have something to share? Please comment below and start a conversation with me