Thursday, January 13, 2011

Classes and Objects

Classes and Objects
It’s difficult to understand class without objects and objects without class. That’s why I will be explaining objects and classes with help of each other. It’s difficult to understand object and classes concept for beginners. For that I will use different real life examples to make it easy for the readers to clear this concept.

Understanding Classes

A class is a model or pattern from which an object is created. The class defines the data types that will be held in an object, and defines code for the functions. After defining a class an object is created from it. The process of creating object from a class is called instantiation. We can also say that class is a map which describes the structure of an object.

For Example. You want to construct a building. First of all you will design a map and will describe its structure. That map is called class. After it you will construct building according to that map. The building you have constructed is called object and will have all those structures describe by its class. The Building can be a hospital, shopping Mall or schools. We can’t live in map because it is not a physical entity but we can live in building because it’s a physical entity.


Structure of Class
Understanding Objects
An object is anything. A class consists of a category of things. An object is a specific item that belongs to a class; it is instance of a class. The class defines the characteristics of its object and the methods that can be applied to its object.
An object contains attributes (i.e. variables) and behaviour (i.e. member functions). Attributes are the data associated with the object and the methods are the functions which operate on the data. Object is the physical entity of the structure described by its class

Consider the example of a class MyCarCompany. The class MyCarCompany describes all car objects (i.e. is attributes and behaviour). You can’t use MyCarCompany class but you can use objects of MyCarCompany. Let suppose objects of MyCarCompany are ObjSkyline, ObjSupra, and ObjGallardo. Now you can use all of these objects of class MyCarCompany. You can’t say that I am driving MyCarCompany instead of it you can say I am driving an ObjSkyline which belongs to MyCarCompany.

Take anonther example, MyDish is a class. You can use the MyDish objects or you can hold MyDish objects and that you can eat from it. MyMug is an object or an instance of the class MyDish. We can use the object of MyDish class but can’t use MyDish class. 



Thats how we instantiate a class. ClassnObject ObjClassnObject = new ClassnObject();
After instantiate we can use attributes and behaviour of class by using dot(.) with Object like I did in picture above. ObjectClassnObject. now you can see small window with some attributes and behaviour that we have define in class. Have you noticed one thing. CreditCard variable is not there because it was private but property of CreditCard is there on top.

















No comments:

Post a Comment