What are Objects , Classes and Encapsulation?

Dictionary Definition of Objects.

  1. A tangible and visible entity. ie. physical objects
  2. Something mental or physical towards which thought, feeling or action is directed. ie conceptual objects

So examples of objects with respect to a college

Physical Objects : buildings, chairs, tables, classrooms

Conceptual Objects :  course, department etc.

The definition of objects can be used to define what  software object is.  A software object is the idea of combining together state (data) and behaviour (operations), that when taken together represents the abstractions of realworld (physical or conceptual) objects .

What is this state and behaviour ?  We will look at them right now.

State :  To capture information about an object, we would require something called data eletements. Lets say that we need to capture information about an employee of an organization.   We would capture information like employee name, address, sex, date of birth, department, designation, salary, educational qualifications etc.   The employee name, address, sex etc are called data elements.  In object oriented terminology, these data elements are called attributes.  This attributes are called as fields in .NET.  These attribute values when taken together determines state or condition of the object. For example, if we want to determine if an employee is eligible for promotion, we need to look a the employees current designation, educational qualifications etc.

Behaviour :  In object oriented terminology, behaviour is something (action) an object does to access the attributes and to modify/maintain the attribute. For example, to know the salary of the employee, the ojbect need to access its salary attribute. When an attribute value is changed, it means that the objects state is changed.  For example, if we change the value of designation attribute to manager, then the state of employee is changed from current state.  Behaviour can also be servicing a request. For example a department object shall ask all the employee object the salary.  

In C# terminology, the behaviour is called as method

What are Classes?

A class is an abstraction describing of common features of all objects in a group of similar objects.  For example, a class called Employee describes the Employee object.

The class describes the following about an object

  1. Attributes and their datatypes
  2. The methods

An object can do only the t hings (methods) that are defined in the class. Similary, an object can only set and access the attributes defined in the class.

Classes can also be viewed as Templates from which an oject is created.  Some even call class as user defined datatype (I am not sure if I will call it like that).

From a class we can create many objects.  The process of creating an object from a class is called as Instantiating.  Since objects are created from a class, objects are also called as Instance of a particular class  

Difference between Class and Object

Classes

Objects

Classes define the attributes and methods. Classes can be thought as templates

Object is an instance of a class with required attributes filled in with values

Encapsulation

Encapsulation is the formal term given in Object terminology to mechanisim of bundling attributes and methods together.

Composition

Composition is the technique of having attribute as reference variable.  That is, an attribute with in a class can hold reference to antoher object.  We normally do this in our class design.  But we dont know what OO calls it.  I am mentioning it here so that I remember it in future as well šŸ™‚

Advertisements

Author: azagappan

Hi.. I am Azagu. I live in Chennai, Tamil Nadu, India. I am a .NET developer. You can reach me at azagappan at gmail.com

3 thoughts on “What are Objects , Classes and Encapsulation?”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s