An object is a complex data type that includes a collection of data fields, methods and properties. It is best to think of objects as real world objects (for the purpose of learning Object-oriented Programming or OOP). Let's say we define a class of objects for a car.
| Data Fields | Mileage, Cylinders, EngineDisplacement |
|---|---|
| Methods | StartEngine, Accelerate, Brake, TurnLeft, TurnRight |
| Properties | Color, Weight, TopSpeed |
You will notice that the Properties and Data Fields are very similar. The difference is that Data Fields represent the underlying data storage and the Properties represent the public interface that developers will use to access an object's data fields.
A class is the definition of a class. You cannot call a method or alter a data field from a class. A class is similar to a typedef in C or a dim statement in Visual Basic. It is simply a declaration of a data type that may be used later to define a class instance.
A class instance is what we refer to as an object. It is a complex variable which represents an instance of a class. An instance is always created with a new keyword and it is destroyed using the delete keyword.
You can think of a class as a category and an object as real instance or example of that class. For example, an automobile is a class while a 2007 Dodge Viper is an object (class instance) of the class. We can define unlimitted number of objects, but there can only be one automobile class in any given application (program).
An object is complex data type that includes a collection of data fields, methods and properties. Object-oriented programming involves three different concepts:
You should make sure you understand each of these concepts before you attempt any object-oriented programming.
Encapsulation, as you may have guessed, provides a way to hide data fields and procedures from the public. They are not physically hidden because anyone who has access to the source code may view them. Access control built into the language will prevent code from reading and writing data fields or calling protected methods.
This seems like a pretty innoculous feature until you realize the true purpose. By hiding data fields and methods, we can publish a standard interface or API that developers can use to access a class object. When new releases of the class definition are designed and built, developers know it's safe to make modifications to hidden fields and methods because those members are not being called or accessed directly.
Inheritance is the process of extending one class definition to define another, derived class. So, we might define a class MotorVehicle as a general class and then want to create some more specific classes which derive from it such as Automobile, Truck, and Motorcycle.
Using inheritance, we can have all of the more specific class definitions inherit or borrow all of the data fields, methods, and properties from the more general class (MotorVehicle). That way, we don't have to redefine common methods and fields inside each specific class.
Of course, you are free to add additional methods and fields to our specific class. These should only be methods and fields that don't belong in the general class. So an Automobile or a Truck might have a property called Airbags whereas the Motorcycle would not.
Polymorphism is the ability for an objects to take on many different forms. Just like we described the many different specific classes that may inherit from one general class, polymorphism means that many specific classes can implement the same method but behave differently when that method is called.
So we may have a general class of Shape, with some very specific classes of Circle, Rectangle and Polygon. All of these specific classes may define a method Draw which displays the shape to the user. The general class may define the Shape class but leave the definition up to the specific classes. Each specific class is free to implement the draw method however they like.
Using Polymorphism, we can define many classes which have the same interface, but behave differently. This is important for manipulating multiple instances where we don't really care about how the method is implemented. We'll see later how objects from different specific classes can be combined in collections and manipulated in batches to simplify development.