Saturday, May 25, 2013

What is an Object?

An object is an instance or representation of a class. Objects are the building blocks of Object Oriented Programming, objects give life to classes and structs. Classes are mere definition of a model,  the model gets activated only when an object is created out of the class.

We can create multiple instances of objects from a single class definition; each object is unique and can hold its own set of values. The fields/properties of an object are used to set/get values to the object while the methods of the object are used to perform some operation on the object.


Let us consider the same Calculator class which we defined in the post What is a Class?, here we shall create an instance of the class, set values to the fields and invoke the methods of the object to see how it operates.

    Calculator objCalculator = new Calculator();
    int nResult = objCalculator.Add(5, 6);
    txtResult.Text = nResult.ToString();

Here objCalculator is the object which represents the class Calculator, the object objCalculator invokes the Add() method of the class and passes values to the method. The object performs the calculation which is defined in its base class definition and returns the result of the calculation, which is captured and displayed in the textbox txtResult in the client application.


Search Flipkart Products:
Flipkart.com

No comments: