The .NET Framework was built to be object oriented from the ground up. What does this mean? For those of you who are unfamiliar with object-oriented programming, here’s a quick review:
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 make 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.
Tip
The .NET Framework offers only single inheritance -- that is, a class may only derive from a single base class. This is different from languages such as C++, which allow classes to be derived from multiple base classes.
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, those unfamiliar with the topic may want to pick up a book that specifically addresses object-oriented programming.
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.
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.
One
of
the coolest things about object orientation in ASP.NET is that you
don’t have to know a thing about how to use it.
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 ASP intrinsic
objects 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.
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.
Get ASP.NET in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.