Understanding the most important terms of OOP - Part II

Learn about class and inheritence

In the previous post, I explained 4 terms of OOP.

  • Object
  • State
  • Method
  • Encapsulation

Now in this post, I am going to continue with the same analogies I gave in my previous post to explain the remaining two.

  • Class
  • Inheritance

What is a Class?

If Object is any thing, then Class is any kind of thing.

Following our previous example: I, “Shankar”, am an object and I come from a kind of things called Man, the class.

1
2
3
4
Shankar ---> Object (thing)
  \
   \
     ------> Man (kind of thing)

My favorite analogy for class is the mould used in Ganesh Chaturti festival, where millions of Ganesha’s idol are created every year on a single day using mould and clay. The process goes like this: you go to one of the many road side shops and ask for an idol. The artist puts a lump of clay into the two piece-mould, closes it and presses hard and then reopens the mould again to get the Ganesha object from it.

Ganesha from mould

The reason I like this analogy is because the mould takes in a lump of clay but when it comes out of the mould, it’s a beautiful object displaying visual characteristics of Ganesha.

Remember our definition of state from the previous post.

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`.

From where does this beauty and shape (state) come to ganesha object? It comes from the mould(class). If ganesha object got its shape from the mould, then the mould actually has defined it i.e., a class defines the states of the object.

The value of state can be different for different objects coming out of the same class, however, a class creates the master template of states whose value an object can change. Object cannot create a new state, it can only modify the value of an existing state. Extending the analogy of ganesha object displayed on the road side, each object is colored differently (different state value) though they come from the same mould but none of the object has a new feature like a third eye or an extra hand (a new state) that is not in the mould (class).

We already saw object encapsulates (encloses if you want an easier term) states and methods. Let’s slightly modify that statement to understand our class better.

Object encapsulates instances of states and methods i.e., to say each object has its own copy of state and method that it got from the class. If this definition of object is understandable, then the class that follows is a natural extension of the same.

Class encapsulates templates of states and methods.

The analogy of ganesha object is a state-only object, so the mould is a state-only class. If you could extend this analogy of mould to some super cool high-tech-AI-infused mould, you can imagine the object that comes out of the mould also displays some behaviors (method) like it can talk, walk and …… (fill in with your imagination).

Inheritance

Let’s revisit our original analogy for object and class.

1
2
3
4
Shankar ---> Object (thing)
  \
   \
     ------> Man (kind of thing)

Man is the class and Shankar is the object and it gets it behavior (methods) from its class. Now let’s focus our attention on the class. Does all state and behavior shankar come purely from Man class? What I mean by “come purely” is does these behavior come directly from Man or does Man re-telecast a behavior from something else?

Let’s zoom our diagram to the class

1
2
3
4
5
6
7
8
9
10
11
....
   \
     --> Man (kind of thing)
         |
         |-----> Unique states eg., (driving_license_number)
         |
         |-----> Unique behaviors eg., (thinking)
         |
         |-----> Inherited state eg., (eyes, nose, teeth)
         |
         |-----> Inherited behaviors (breathing, eating, digesting, running)

A class inherits a lot of state and behavior templates which it happily passes to the objects it creates. From where does a class inherit? It inherits from another class which in turn may inherit again.

1
Living Being > Animal Kingdom > Human Being > Man > shankardevy

The above is an inheritance chain of the object ‘shankar’. Shankar has driving_license_number state from the Man class but the breathing behavior from Living Being class. Because of these inheritance, Man class can just define templates for states and methods for what is unique in man and just inherit the rest from other classes. This helps Man class to stay lean and manageable.

In the next post, I will explain how to use these vocabulary of OOP in real work.


comments powered by Disqus