1.2 Object Orientation in the .NET Platform
We've already discussed classes. Classes are the blueprints or templates from which objects are created. Objects, the heart of object-oriented programming, are usable instances of a class. Objects expose properties, which contain data related to or about the object, and/or methods, which allow actions to be performed on the object.
In object-oriented programming, objects need to support three important qualities: encapsulation, inheritance, and polymorphism.
Encapsulation refers to the ability of an object to hide its internal data from outside view and allow access to only that data through publicly available methods. This helps prevent clients from accidentally or purposefully leaving object data in a corrupt state and makes it easier for the developer of the class on which the object is based to change the internal implementation of these data members without breaking its clients.
Inheritance refers to the ability to derive one class from another. This allows developers to create a new class based on an existing class. The new class inherits all methods and properties of the existing class. The developer can then add new methods or properties or override existing methods. Inheritance allows you to develop specialized versions of objects that are customized to meet your precise needs. We'll discuss this type of scenario more in Chapter 6.
Polymorphism refers to the ability of multiple classes derived from the same base class to expose methods with the same name—all of which clients can call in exactly the same way, regardless of the underlying implementation. Thus, a Car class could expose a Start method and a derived class SportsCar could override that Start method to provide a different implementation. From the client's perspective, however, both methods are used the same way.
This is a very high-level overview of object-oriented programming. While we'll discuss object-oriented techniques in more depth throughout the book, if you are unfamiliar with the topic you may want to pick up a book that specifically addresses object-oriented programming.
1.2.1 Why Is It Important? Rapid Development and Reuse!
What's important about the object-oriented nature of the .NET platform is that it allows much faster development than did previous generations of Windows development technologies and offers much greater opportunities for reuse.
Because the functionality of the .NET Framework is exposed as a set of object- oriented classes rather than a set of obscure and finicky API calls, many operations that were difficult or downright impossible in classic ASP are simple in ASP.NET. For example, about ten lines of code can perform a DNS lookup on a domain name using the classes in the System.Net and System.Net.Sockets namespaces. This task wasn't even possible in classic ASP, without the use of external components.
What's more, because many classes in the .NET framework can be used as base classes, it is easy to reuse them in your own applications by deriving from a class to provide common functionality and then extending the derived class to add functionality specific to your application. In fact, much of the .NET Framework is built this way. For example, all classes that make up the ASP.NET Server Controls are ultimately derived from the Control class of the System.Web.UI namespace, which provides properties and methods common to all server controls.
1.2.2 OO Is at the Heart of Every ASP.NET Page
One of the coolest things about object orientation in ASP.NET is that you don't have to know much about how to use it since most of it is under the covers for basic page development. Every ASP.NET page implicitly inherits from the Page class of the System.Web.UI namespace, which provides access to all ASP.NET implementations of the intrinsic objects that were introduced in classic ASP, such as Request, Response, Session, and Application, and to a number of new properties and methods. One advantage of this is that each page is compiled into an assembly based on the Page class, providing substantial performance improvements over classic ASP, in which code was interpreted at runtime.
Object orientation is also the key to another important new feature of ASP.NET: code-behind. Code-behind allows developers to separate executable code from the HTML markup that makes up the user interface. Executable code is placed in a module called a code-behind file, which is associated with the ASP.NET page via an attribute in the page. The code-behind file contains a class that inherits from the Page class. The ASP.NET page then inherits from the code-behind class, and at runtime, the two are compiled into a single executable assembly. This compilation allows a combination of easy separation of UI and executable code at design time with high performance at runtime.