Object Oriented Programming, Part 2: Understanding Objects

By David Giard


Before we can do that, it’s important to understand the basics of objects before you can grasp Object Oriented Programming.
Key Object Concepts
Objects are essentially a collection of structured data stored in memory. An object is based on a class that defines how to create an object.
In this article, I will describe the following concepts.
·         Classes
·         Class members
o   Properties and Fields
o   Methods
o   Events
·         Instances
·         Static Types
·         Interfaces
·         Message Passing
Because all these terms are interrelated, it is difficult to discuss one without mentioning the others. So be patient if I mention a term before I define it – I will get to the definition shortly.
Classes
A class is a definition for an object. It describes the attributes such as properties, fields and methods (more on this later) of an object. It may also set default values and implementations for these attributes. Think of a class as a blueprint we can use to help us build an object. Generally, we work with classes only at design-time, defining the attributes appropriate for that class. In most cases, we do not work directly with classes while a program is running.
A class is often represented by a UML class diagram, such as the one below.
Public Members
A class (and an object) exposes a finite set of publicly-exposed members. Members are methods, properties and fields (defined below). This public interface is how you interact with an object. An object may be capable of far more than what it exposes by its public interface, but these other capabilities are not seen directly by the outside world. Keeping object interfaces simple is one way that an object can help simplify a complex system.
Properties and Fields
Properties and fields describe static data associated with a class.
A property is similar to a field, but a property provides a “Getter” and “Setter” – methods that run when you attempt to read or write the value of a property. This allows a developer to add code that will automatically run whenever a field’s value is retrieved or assigned. This code might perform validation or calculate a value on the fly.
Methods
A method is a discrete section of code that is associated with a class and therefore with an object. Some methods return a value; others just run code and return nothing.
Instances
So far, we’ve been talking about objects without defining what an object is. An object is an instance of a class – it represents a specific set of data.
We said before that a class is like a blueprint. Think of an object as the house, machine or other device built from that blueprint.  Methods, properties and fields defined in a class become methods, properties and fields in any object based on that class.
It is possible to produce more than one house from the same blueprint. Similarly, it is possible to create multiple objects from the same class.
Static Types
It is possible to instantiate a class without explicitly creating an instance of that class. You can do this if the class is defined as “static”.
For static classes, the .Net framework takes care of instantiating an object for us. Static types require and use only one instance of a class.
Constructors
A constructor is a special type of method that runs when an object is first created. This is a good place to put initialization code for your object. 
You may create constructors that accept parameters. This is useful if you want to set the values of an object’s properties at the time you instantiate that object.
Events
An event is a notification by an object that something has happened. This something might be user input, such as a mouse-click on a form; or it can be something less tangible, such as a customer exceeding his credit limit. Other objects may or may not respond to these events. It is up to these other objects to subscribe to an event if they wish to respond to that event. Generally the object and raising event do not know how the event will be consumed.
Interfaces
An interface looks like a class in that it can have properties, fields and methods. The difference is that the properties and methods contain no implementation code. Interfaces are used only to define the public members of a class. A class implementing an interface inherits all the public members of that interface and it is up to the class to provide implementation.
Message Passing
Objects communicate by passing messages. These messages can be primitive data types, such as strings and integers; they can be XML; they can be binary data; or they can be other objects. Generally speaking objects expose public members to accept these messages. This helps to simplify the communication between objects.
In this article, we learned the basics of objects, their definitions, their members and how to work with them. In the next article, we’ll introduce Object Oriented Programming constructs and how these concepts are implemented using objects.
 
David Giard has been developing solutions using Microsoft technologies since 1993. In the past, he has spoken at Day of .Net, CodeStock, Microsoft DevCares, Microsoft ArcReady, Dot Net University, X Conference and numerous user groups around the Midwest.  He is a recovering certification addict and holds an MCTS, MCSD, MCSE, and MCDBA, as well as a BS and an MBA. He is the host and producer of the popular online TV show Technology and Friends. He is an officer of the Great Lakes Area .Net User Group. He is an avid photographer and has visually documented many of the Heartland community events. You can read his latest thoughts at www.DavidGiard.com
David lives in Michigan with his two teenage sons