Thursday 18 August 2011

Objects vs Classes




Objects and classes are used in object oriented programming languages. All object oriented programming languages such as C++, Java, .NET and others, employs objects and classes.


Objects


An object is defined as any entity that can be utilized by using commands in a programming language. Object can be a variable, value, data structure
or a function. In object oriented environment, object is referred to as
instance of a class. Objects and classes are closely related to each
other. In real world, the objects are your TV, bicycle, desk and other
entities. Methods are used to access the objects of a class. All the
interaction is done through the object’s methods. This is known as data
encapsulation. The objects are also used for data or code hiding.


A number of benefits are provided by the objects when they are used in the code:


• Ease of debugging –
The object can be easily removed from the code if there is some problem
due to it. A different object can be plugged in as a replacement of the
former one.


• Information hiding
– The code or internal implementation is hidden from the users when
interaction is done through object’s methods.


• Reuse of code – if
an object or code is written by some other programmer then you can also
use that object in your program. In this way, objects are highly
reusable. This allows experts to debug, implement task specific and
complex objects that can be used in your own code.


• Modularity – You
can write as well as maintain the source codes of objects in an
independent manner. This provides modular approach to programming.


Classes


A class is a concept used in object
oriented programming languages such as C++, PHP, and JAVA etc. Apart
from holding data, a class is also used to hold functions. An object is
an instant of a class. In case of variables, the type is the class
whereas the variable is the object. The keyword “class” is used to
declare a class and it has the following format:


class CLASS_NAME


{


AccessSpecifier1:


Member-1;


AccessSpecifier2:


Member-2;




} OBJECT_NAMES;


Here, the valid identifier is CLASS_NAME
and the names for objects are represented by OBJECT_NAMES. The benefit
of objects include information hiding, modularity, ease in debugging and
reuse of the code. The body contains the members that can be functions
or data declarations. The keywords for access specifiers are public,
protected or private.


• The public members can be accessed anywhere.


• The protected members can be accessed within same classes or from friend classes.


• The private members can be accessed only within the same class.


By default, the access is private when the class keyword is used. A class can hold both data and functions.







Objects vs. Classes


• An object is an instant of a class. A class is used to hold data and functions.


• When a class is declared, no memory is
allocated but when the object of the class is declared, memory is
allocated. So, class is just a template.


• An object can only be created if the class is already declared otherwise it is not possible


No comments:

Post a Comment