Classes and Objects

Now that we have a fair idea about OOAD, let’s get into some fundamental aspects of object orientation. The topic discussed in this section is very simple and it is something that we do every day.

 
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 traditional definition, a class is a collection of data and methods.

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

    We have already discussed during systems principles and patterns that every system will have a structure and every system will have behaviour.

    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 behaviour of any object that should be called of a particular Class or Type.


    We know that structurally, components can be 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 behaviour is represented by the methods of the component.

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

     
    Importance of Classes to software systems
     

    After knowing about classes, what is the next thing that should come to our mind?

    Any guesses?

    Yes. You got it right.

    The next thing that comes to our mind is

     
    "What is the need for writing a class within software systems"?
     

    We keep writing a lot of classes in our programming profession. But what is the need?

    Let me give you a clue which will help you find an answer to this question.

    Can you think about the base English word from which the keyword "Class" is derived?

     
    You are right … it's Classification.
     

     
    Classification very simply means "categorization of real world objects on the basis of structural and behavioural characteristics of that object".
     

    Let me give you a very interesting example.

    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 you say that the first object is a Human, the second object is a monkey and the third object is a car?
     

     
    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.

  • The classification is 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.
  • We are aware of 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 behaviour for anyone who should be called a human being, a monkey or a car.

    As per the fundamental definition of a System,

     
    "Every system will offer at least one behaviour and that behaviour will be used by at least one more system in this world".
     

     
    And how do we get a Behaviour from a system?
     

    We know that structural components within the system interact with each other to give a behaviour offered by the system.

     
    What is the prerequisite for one object to interact with the other object?
     

    The calling object should know the type of the target object to interact with the target object.

    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. And if the objects cannot interact with each other, the entire concept of systems will collapse.

    According to me, this world without classification is like millions and billions of objects doing nothing.

    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.
     


     
    What is a Software System?
     

    According to me,

     
    "A Software system is an automation system, meant for automating the complexities of the real world systems by using the hardware in the most optimal form."
     

     
    What are the prerequisites for making an automated system behave as naturally as a real world system?
     

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

    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 resulting in the failure of the system.

    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 behaviours offered by the object.
     

     
    What is an Object?
     

     
    "Object is an instance of a class."
     

    Let's first discuss the fundamental characteristics of an object and then compare the real world and software objects through an analogy.

    Let's first discuss the technical characteristics of a real world object, a Human being.

     
    Technical characteristics of a Human Being (Real World object)
     

    The class "Human" defines the structure and behaviour of an object that can be called as a Human.
    The moment a baby is born, an object of class "Human" is instantiated and the following are the properties of that baby.

  • The baby has a physical structure.
  • The baby occupies a certain space in the world.
  • There are certain values which are stored in the structure, which defines the state of the baby at a particular instance.
  • The baby has a unique identifier.
  • The baby exhibits the structure and behaviour defined by the class.
  • From the time of birth, if any object wants to interact with the baby, the prerequisite is that it (calling entity) should know the physical address of the baby as to where it is located.
  • Once the physical address is known, the calling entity can safely interact with the baby.

    All the above mentioned characteristics of the baby are also true for a software object belonging to a particular class.

    Now let's talk about the technical characteristics of a Software Object.

     
    The following describes the technical characteristics of a software object.
     

  • The object has a physical structure.
  • The moment the object is created, instead of occupying a space in the world, it occupies a space in the memory.
  • The amount of memory allocated depends on the data structure and the data types used.
  • The state of the object at a particular instance is defined by certain values which are stored in the structure.
  • Every object has a unique identifier.
  • The object exhibits the structure and behaviour defined by the class.
  • From the point of instantiation, if anyone would like to interact with the object, the prerequisite is the calling entity should know the address of the memory location allocated to the object.
  • Once the address is known, the calling entity can safely interact with the object.

     
    The above analogy shows one to one similarities between a real world and a software object. The only difference being, in the real world we deal with physical locations while in software we deal with memory locations.
     

    Whatever is true for the human baby, the same is true with the software object as well.

    The moment you say that an object of a class is instantiated, what happens? Like a baby, every object has a physical structure. Instead of occupying a physical space in the world, the object will occupy a space in the memory.

    And how much memory will this object occupy?

    Depending upon the Data type and Data structures that you have used within the corresponding class. That much amount of memory will be allocated to the corresponding object.

    Because every object has a physical structure and because it is occupying certain memory locations, there are some values which are stored in that structure. And that is nothing but the state of the object at that point of time.

    Every object will have a unique identifier. Needless to say, the object will exhibit the structure and behaviour defined by the corresponding class.

    And finally, from the point of instantiation, if anyone would want to interact with that object, the pre-requisite is that you should reach the memory location where the object is located. If you can reach the location where the object is located, you can interact with the object and get your work done.

    The reason I gave you this analogy is for you to understand the difference between the real world and software systems which is object oriented. The only difference is that in one case you deal with physical locations and in the other case you deal with memory locations. Most of the other things are the same.

 

 
Hemant Jha
Founder - VPlanSolutions
Researcher, Trainer

www.VPlanSolutions.co.in