Lesson 1: Introduction to Object-Oriented Programming

Lesson 1: Introduction to Object-Oriented Programming

Programming in the .NET Framework environment is done with objects. Objects are programmatic constructs that represent packages of related data and functionality. Objects are self-contained and expose specific functionality to the rest of the application environment without detailing the inner workings of the object itself. Objects are created from a template called a class. The .NET base class library provides a set of classes from which you can create objects in your applications. You also can use the Microsoft Visual Studio programming environment to create your own classes. This lesson introduces you to the concepts associated with object-oriented programming.

After this lesson, you will be able to

  • Describe the members of an object

  • Describe the difference between an object and a class

  • Explain what is meant by object model

  • Explain what is meant by abstraction, encapsulation, and polymorphism

Estimated lesson time: 20 minutes

Objects, Members, and Abstraction

An object is a programmatic construct that represents something. In the real world, objects are cars, bicycles, laptop computers, and so on. Each of these items exposes specific functionality and has specific properties. In your application, an object might be a form, a control such as a button, a database connection, or any of a number of other constructs. Each object is a complete functional unit, and contains all of the data and exposes all of the functionality required to fulfill its purpose. The ability of programmatic objects to represent real-world objects is called abstraction.

Classes Are Templates for Objects

Classes were discussed in Chapter 1 and represent user-defined reference types. Classes can be thought of as blueprints for objects: they define all of the members of an object, define the behavior of an object, and set initial values for data when appropriate. When a class is instantiated, an in-memory instance of that class is created. This instance is called an object. To review, a class is instantiated using the New (new) keyword as follows:

Visual Basic .NET

' Declares a variable of the Widget type Dim myWidget As Widget ' Instantiates a new Widget object and assigns it to the myWidget ' variable myWidget = New Widget()

Visual C#

// Declares a variable of the Widget type Widget myWidget; // Instantiates a new Widget object and assigns it to the myWidget // variable myWidget = new Widget();

When an instance of a class is created, a copy of the instance data defined by that class is created in memory and assigned to the reference variable. Individual instances of a class are independent of one another and represent separate programmatic constructs. There is generally no limit to how many copies of a single class can be instantiated at any time. To use a real-world analogy, if a car is an object, the plans for the car are the class. The plans can be used to make any number of cars, and changes to a single car do not, for the most part, affect any other cars.

Objects and Members

Objects are composed of members. Members are properties, fields, methods, and events, and they represent the data and functionality that comprise the object. Fields and properties represent data members of an object. Methods are actions the object can perform, and events are notifications an object receives from or sends to other objects when activity happens in the application.

To continue with the real-world example of a car, consider that a Car object has fields and properties, such as Color, Make, Model, Age, GasLevel, and so on. These are the data that describe the state of the object. A Car object might also expose several methods, such as Accelerate, ShiftGears, or Turn. The methods represent behaviors the object can execute. And events represent notifications. For example, a Car object might receive an EngineOverheating event from its Engine object, or it might raise a Crash event when interacting with a Tree object.

Object Models

Simple objects might consist of only a few properties, methods, and perhaps an event or two. More complex objects might require numerous properties and methods and possibly even subordinate objects. Objects can contain and expose other objects as members. For example, the TextBox control exposes a Font property, which consists of a Font object. Similarly, every instance of the Form class contains and exposes a Controls collection that comprises all of the controls contained by the form. The object model defines the hierarchy of contained objects that form the structure of an object.

An object model is a hierarchical organization of subordinate objects contained and exposed within a main object. To illustrate, let s revisit the example of a car as an object. A car is a single object, but it also consists of subordinate objects. A Car object might contain an Engine object, four Wheel objects, a Transmission object, and so on. The composition of these subordinate objects directly affects how the Car object functions as a whole. For example, if the Cylinders property of the Engine subordinate object is equal to 4, the Car will behave differently than a Car whose Engine has a Cylinders property value of 8. Contained objects can have subordinate objects of their own. For example, the contained Engine object might contain several SparkPlug objects.

Encapsulation

Encapsulation is the concept that implementation of an object is independent of its interface. Put another way, an application interacts with an object through its interface, which consists of its public properties and methods. As long as this interface remains constant, the application can continue to interact with the component, even if implementation of the interface was completely rewritten between versions.

Objects should only interact with other objects through their public methods and properties. Thus, objects should contain all of the data they require, as well as all of the functionality that works with that data. The internal data of an object should never be exposed in the interface; thus, fields rarely should be Public (public).

Returning to the Car example. If a Car object interacts with a Driver object, the Car interface might consist of a GoForward method, a GoBackward method, and a Stop method. This is all the information that the Driver needs to interact with the Car. The Car might contain an Engine object, for example, but the Driver doesn t need to know about the Engine object all the Driver cares about is that the methods can be called and that they return the appropriate values. Thus, if one Engine object is exchanged for another, it makes no difference to the Driver as long as the interface continues to function correctly.

Polymorphism

Polymorphism is the ability of different classes to provide different implementations of the same public interfaces. In other words, polymorphism allows methods and properties of an object to be called without regard for the particular implementation of those members. For example, a Driver object can interact with a Car object through the Car public interface. If another object, such as a Truck object or a SportsCar object, exposes the same public interface, the Driver object can interact with them without regard to the specific implementation of that interface. There are two principal ways through which polymorphism can be provided: interface polymorphism and inheritance polymorphism.

Interface Polymorphism

An interface is a contract for behavior. Essentially, it defines the members a class should implement, but states nothing at all about the details of that implementation. An object can implement many different interfaces, and many diverse classes can implement the same interface. All objects implementing the same interface are capable of interacting with other objects through that interface. For example, the Car object in the previous examples might implement the IDrivable interface (by convention, interfaces usually begin with I), which specifies the GoForward, GoBackward, and Halt methods. Other classes, such as Truck, Forklift, or Boat might implement this interface and thus are able to interact with the Driver object. The Driver object is unaware of which interface implementation it is interacting with; it is only aware of the interface itself. Interface polymorphism is discussed in detail in Lesson 3.

Inheritance Polymorphism

Inheritance allows you to incorporate the functionality of a previously defined class into a new class and implement different members as needed. A class that inherits another class is said to derive from that class, or to inherit from that class. A class can directly inherit from only one class, which is called the base class. The new class has the same members as the base class, and additional members can be added as needed. Additionally, the implementation of base members can be changed in the new class by overriding the base class implementation. Inherited classes retain all the characteristics of the base class and can interact with other objects as though they were instances of the base class. For example, if the Car class is the base class, a derived class might be SportsCar. The SportsCar class might be the base class for another derived class, the ConvertibleSportsCar. Each newly derived class might implement additional members, but the functionality defined in the original Car class is retained. Inheritance polymorphism is discussed in detail in Lesson 4.

Lesson Summary

  • Abstraction is the representation of real-world objects as programmatic constructs. Programmatic objects can represent real-world objects through their implementation of members.

  • Classes are the blueprints for objects. When an object is created, a copy of the class is created in memory, and values for member variables are initialized. A class can act as a template for any number of distinct objects.

  • Encapsulation is a principle of object-oriented programming. An object should contain all of the data it requires and all of the code necessary to manipulate that data. The data of an object should never be made available to other objects. Only properties and methods should be exposed in the interface.

  • Polymorphism is the ability of different objects to expose different implementations of the same public interface. Two major types of polymorphism exist in Visual Basic .NET and Visual C#:

    • Interface polymorphism.

      An interface defines a contract for behavior. It specifies what members must be implemented, but provides no details as to their implementation. An object can implement many unrelated interfaces, and many diverse objects can implement the same interface.

    • Inheritance polymorphism.

      Objects can inherit functionality from one another. An inherited class retains the full implementation of its base class, and instances of inherited classes can be treated as instances of the base class. Inherited classes can implement additional functionality as required.



MCAD(s)MCSD Self-Paced Training Kit(c) Developing Windows-Based Applications With Microsoft Visual Basic. Net a[.  .. ]0-316
MCAD(s)MCSD Self-Paced Training Kit(c) Developing Windows-Based Applications With Microsoft Visual Basic. Net a[. .. ]0-316
ISBN: 735619263
EAN: N/A
Year: 2003
Pages: 110

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net