What is a class and why are they needed?

In this article I will discuss the “Need behind writing classes”. My experience of teaching shows me that most of engineers are not really aware of this need and hence they will find this article interesting.

The need behind the usage of classes is dependent on a lot of basic system principles and patterns. Although the description is a bit lengthy but I sure it will be worth the time spent.

Before I can give you my opinion of the need behind the need of classes let me very quickly address the question “What is a class?”

In my training programs, whenever I pose this question, I get numerous answers like

  • Class is a User Defined Data type.
  • Class is a collection of data and methods.
  • Class is a template of a real world entity.
  • Class is a blue print of a real world object.
  • Class is a collection of objects.

The first 4 definitions are technically correct, while the last one is definitely not right.

Let me give you my perspective of this concept called as Class.

Let me try and enlarge the scope of the definition of a Class from the traditional definition of classes.

According to the usual definition, “a class is a collection of data and methods.”

According to me "A Class is a representation of the structure and behavior of any component at any level of granularity".

As we know every system will atleast have one behavior and will have some enabling structure to deliver the behavior.

So when we say that a class is a representation, it means that class is a conceptual entity and has no physical existence. The class just talks about as to what should be the structure and behavior of any object that should be called of a particular Class or Type.

I presume we all would know that within a system the structural components are nested at different levels of granularity.

For the component that is at the lowest level of granularity, the structure is represented by the attributes while the behavior is represented by the methods of the component. While for the component that is at the enclosing level of granularity i.e. systems or subsystems, the structure is represented by the enclosed components or parts within this enclosing component while the behaviors are offered by this enclosing component.

The above description shows that a Class is a much broader concept and by saying that a class is a collection of attributes and methods we are limiting its scope. We have broadened the concept of a class so as to take care of systems and as well as the subsystems. A class is a much broader concept which can encompass “Systems” and Subsystems as well. We can have classes of systems and subsystems as well.

As we now know “WHAT” is a class.

Now let us address the core question “Why” are classes needed?

"What is the need for writing a class within software systems" or Why do we write classes?

We keep writing a lot of classes in our programming profession. As I have mentioned previously this is one question to which I have rarely got a right answer over my 5 years of training experience.

Let me start by giving a clue which will help you find an answer to this question.

What is the base English word from which the keyword "Class" is derived?

The keyword “Class” is derived from the base English word “Classification”.

As we all know Classification very simply means "categorization of real world objects on the basis of structural and behavioral characteristics of that object".

Now let me put across one more interesting question “Can we envision/define this world without classification?”

Let me put across a very interesting example.

lets say I place three real world objects in front of you;

  • Object # 1- A Human being
  • Object # 2- A Monkey
  • Object # 3- A Car

Now my question is what makes anyone say that the first object is of type Human Being, the second object is of type Monkey and the third object is of type Car?

If we are able to visualize each of these objects individually, then there has to some prerequisites that has to be true for each one of us to identify these objects individually.

What are the prerequisites for you to identify these 3 objects distinctly?

The pre requisites for a person to identify any of these real world objects are as follows.

  1. The classification should have been already done. I.e. someone has already done the classification as to what should be the structure and behavior of any real world object that can be qualified as a human being, a monkey or a car.
  2. We are aware of this classification. If we realize from class 1 to class 8 we only learn this classification.

Please understand that if we are able to identify these 3 objects today, it is because someone has already done the classification for us, stating as to what should be the structure and behavior for anyone who should be called a human being, a monkey or a car.

As per the fundamental definition of a System,

“Every system in this world will give atleast one behavior.” and “If the system is offering atleast one behavior, there will be one more system in this world to use that behavior” (More details wrt this principle can be found in the section “Fundamental properties of a system” on the following link  http://www.hemantjha.in/content/3/fundamental-properties-of-a-system.html)

We also know that structural components within the system interact with each other to give a behavior offered by the system. As we know component interaction is the technical cause behind the execution of behaviors within systems. (More details wrt this principle can be found in the section “Implementation of Behavior” on the following link http://www.hemantjha.in/content/3/implementation-of-behavior.html )

So we should also know the prerequisites for one object to interact with other object?

The following the prerequisites

  1. Target system or the called object should exist.
  2. The calling object should know the address of the called object.
  3. The calling object should know the type of the called object.

Now ask your self , in the real world will you interact with any object without knowing the class to which the object belongs?

Now if there is no classification, the objects will not be able to identify each other and hence will not be able to interact with each other. While if the objects cannot interact with each other, the entire concept of systems will collapse. We just cannot define the concept of “System” .Look into the two fundamental principles of the systems described above.

If I was asked to describe this world without “Classification”, I will describe it as “millions and billions of objects doing nothing”. I am saying so because of the objects cannot identify each other and hence cannot interact with each other .. then what will they do?

To put it in simple words, I can say that “Systems cannot exist without classification” or “Classification within the systems is a prerequisite for systems to exist.”

Till this point I have just described the need of “Classification” in this real world.

Now let us come back to our software systems.

Before I can explain the need behind writing classes , let us quickly understand software systems

According to me

“A Software System is a particular type of system which is meant to automate the complexity of real world systems by using the processing capability (of the processor), the storage capability (of the memory) and the transmission capability of the hardware available to us today, there by adding a value to its end users and enabling behaviors which are not possible manually.”

The detailed explanation of the above mentioned definition can be found in the article “Software systems - Automating the complexity of the real world” on the following link.

This definition plays a very crucial role in clearing a lot of myths about software systems so I will definitely suggest you to read the article.

As we know software systems are automation systems.

The following are the prerequisites for any automation system to behave as naturally as the real world system.

  1. The structural and behavioral complexity of the existing real world system has to be understood.
  2. We should be able to replicate or represent similar or better structural components in the automated system.
  3. Make the structural components within the automated system interact with each other, the way they interact in the real world system.
  4. Use the hardware in the most optimum form to add value to the end user in terms of speed and scale.

(More details about automation systems can be found in the section “Problem with Humanoid Robots” on the following link http://www.hemantjha.in/content/3/problem-with-humanoid-robots.html )

The moment we implement step number 2 and replicate similar or better structural components in the automated system.

Ask yourself ..

If for the real world systems, classification is a prerequisite, then classification is also a prerequisite for the software systems which are just automating the real world systems.

While trying to map the structural components of the real world system within the software system, if there is no classification done, it will lead to the problem of objects not being able to identify each other and hence will not be able to interact with each other. And if the replicated structural components cannot interact with each other, I hope you will realize the entire concept of systems will fail.

Coming back to why Classes are needed within software systems?

Classes are needed or written within software systems to provide a classification for the software objects to identify each other so that they can interact and deliver the behaviors offered by the resultant software system.

This article is the first step to highlight the fact that most of the programming languages are designed to take care of everything that’s possible in the real world as naturally as possible, for things which cannot be replicated as it is workarounds are created to simulate the real world characteristics as much as possible.

Hemant Jha
Founder - VPlanSolutions
Researcher, Trainer