Visual Basic .NET is a fully object-oriented programming language, which means it
supports the four basic tenets of object-oriented programming: abstraction, encapsulation,
inheritance, and polymorphism.
We have already conceptualized many of these object-oriented concepts by just looking
at the objects that surround us in our everyday lives. Let’s look more closely at
these terms and see what they actually mean and what they do for developers of
object-oriented software.
Abstraction
In object-oriented software, complexity is managed by using abstraction. Abstraction
is a process that involves identifying the crucial behavior of an object and eliminating
irrelevant and tedious details. A well thought-out abstraction is usually
simple, slanted toward the perspective of the user (the developer using your objects),
and has probably gone through several iterations. Rarely is the initial attempt at an
abstraction the best choice.
Remember that the abstraction process is context sensitive. In an application that
will play music, the radio abstraction will be completely different from the radio
abstraction in a program designed to teach basic electronics. The internal details of
the latter would be much more important than the former.
Encapsulation
In contrast, object-oriented programming is based on encapsulation. When an
object’s state and behavior are kept together, they are encapsulated. That is, the data
that represents the state of the object and the methods (Functions and Subs) that
manipulate that data are stored together as a cohesive unit.
Encapsulation is often referred to as information hiding. But although the two terms
are often used interchangeably, information hiding is really the result of encapsulation,
not a synonym for it. They are distinct concepts. Encapsulation makes it possible
to separate an object’s implementation from its behavior—to restrict access to its
internal data. This restriction allows certain details of an object’s behavior to be hidden.
It allows us to create a “black box” and protects an object’s internal state from
corruption by its clients.
Encapsulation is also frequently confused with abstraction. Though the two concepts
are closely related, they represent different ideas. Abstraction is a process. It is
the act of identifying the relevant qualities and behaviors an object should possess.
Encapsulation is the mechanism by which the abstraction is implemented. It is the
result. The radio, for instance, is an object that encapsulates many technologies that
might not be understood clearly by most people who benefit from it.
In Visual Basic .NET, the construct used to define an abstraction is called a class.
The terms class and object are often used interchangeably, but an object is actually an
instance of a class. A component is a collection of one or more object definitions, like
a class library in a DLL.
Inheritance
Inheritance is the ability to define a new class that inherits the behaviors (and code)
of an existing class. The new class is called a child or derived class, while the original
class is often referred to as the parent or base class.
Inheritance is used to express “is-a” or “kind-of” relationships. A car is a vehicle. A
boat is a vehicle. A submarine is a vehicle. In OOP, the Vehicle base class would provide
the common behaviors of all types of vehicles and perhaps delineate behaviors
all vehicles must support. The particular subclasses (i.e., derived classes) of vehicles
would implement behaviors specific to that type of vehicle. The main concepts
behind inheritance are extensibility and code reuse.
of an existing class. The new class is called a child or derived class, while the original
class is often referred to as the parent or base class.
Inheritance is used to express “is-a” or “kind-of” relationships. A car is a vehicle. A
boat is a vehicle. A submarine is a vehicle. In OOP, the Vehicle base class would provide
the common behaviors of all types of vehicles and perhaps delineate behaviors
all vehicles must support. The particular subclasses (i.e., derived classes) of vehicles
would implement behaviors specific to that type of vehicle. The main concepts
behind inheritance are extensibility and code reuse.
In contrast to inheritance, there is also the notion of a “has-a” relationship. This relationship
is created by using composition. Composition, which is sometimes referred
to as aggregation, means that one object contains another object, rather than inheriting
an object’s attributes and behaviors. When it comes to proper object-oriented design, a deep understanding of inheritance
and its effects is crucial. Deriving new classes from existing class that is known as inheritance.
is created by using composition. Composition, which is sometimes referred
to as aggregation, means that one object contains another object, rather than inheriting
an object’s attributes and behaviors. When it comes to proper object-oriented design, a deep understanding of inheritance
and its effects is crucial. Deriving new classes from existing class that is known as inheritance.
Polymorphism
Polymorphism refers to the ability to assume different forms. In OOP, it indicates a
language’s ability to handle objects differently based on their runtime type.
When objects communicate with one another, we say that they send and receive messages.
The advantage of polymorphism is that the sender of a message doesn’t need
to know which class the receiver is a member of. It can be any arbitrary class. The
sending object only needs to be aware that the receiving object can perform a particular
behavior.
A classic example of polymorphism can be demonstrated with geometric shapes.
Suppose we have a Triangle, a Square, and a Circle. Each class is a Shape and each
has a method named Draw that is responsible for rendering the Shape to the screen.
With polymorphism, you can write a method that takes a Shape object or an array of
Shape objects as a parameter (as opposed to a specific kind of Shape). We can pass
Triangles, Circles, and Squares to these methods without any problems, because
referring to a class through its parent is perfectly legal. In this instance, the receiver is
only aware that it is getting a Shape that has a method named Draw, but it is ignorant
of the specific kind of Shape. If the Shape were a Triangle, then Triangle’s version of
Draw would be called. If it were a Square, then Square’s version would be called, and
so on.
This type of polymorphism is called parametricpolymorphism , or generics. Another
type of polymorphism is called overloading. Overloading occurs when an object has
two or more behaviors that have the same name. The methods are distinguished only
by the messages they receive (that is, by the parameters of the method).
Polymorphism is a very powerful concept that allows the design of amazingly flexible
applications.
.NET Framework
Two major elements of the .NET Framework will be addressed repeatedly throughout
this book. The first is the Common Language Runtime (CLR), which provides
runtime services for components running under .NET.
The second element is the .NET class library, a vast toolbox containing classes for
everything from data access, GUI design, and security to multithreading, networking,
and messaging.
The library also contains definitions for all primary data types,
such as bytes, integers, and strings. All of these types are inherently derived from a
base class called System.Object, which you can think of as a “universal” data type;
there is no distinction between the types defined by the system and the types you create
by writing classes or structures. Everything is an object!
The term .NET means many things to many different people. When
the term is used in this book, it always refers to the .NET Framework—
the Common Language Runtime and the .NET class library.
In the past, passing a string from a component written in VB to one written in C++
(or vice versa) could be frustrating. Strings in VB weren’t the same as the strings in
The Common Language Runtime
such as bytes, integers, and strings. All of these types are inherently derived from a
base class called System.Object, which you can think of as a “universal” data type;
there is no distinction between the types defined by the system and the types you create
by writing classes or structures. Everything is an object!
The term .NET means many things to many different people. When
the term is used in this book, it always refers to the .NET Framework—
the Common Language Runtime and the .NET class library.
In the past, passing a string from a component written in VB to one written in C++
(or vice versa) could be frustrating. Strings in VB weren’t the same as the strings in
The Common Language Runtime
The CLR is the execution engine for the .NET Framework. This runtime manages all
code compiled with VB.NET. In fact, code compiled to run under .NET is called
managed code to distinguish it from code running outside of the framework.
Besides being responsible for application loading and execution, the CLR provides
services that will benefit component developers:
• Invocation and termination of threads and processes
• Object lifetime and memory management
• Cross-language integration
• Code access and role-based security
• Exception handling (even across languages)
• Deployment and versioning
• Interoperation between managed and unmanaged code
• Debugging and profiling support (even across languages)
Runtimes are nothing new. Visual Basic has always had some form of a runtime.
Visual C++ has a runtime called MSVCRT.DLL. Perl, Python, and SmallTalk also
use runtimes. The difference between these runtimes and the CLR is that the CLR is
designed to work with multiple programming languages. Every language whose compiler
targets the .NET Framework benefits from the services of the CLR as much as
any other language.
.NET is also similar to Java. Java uses a runtime called the Java Virtual Machine. It
can run only with Java code, so it has the same limitations as the other languages
mentioned previously. Another distinction is that the JVM is an interpreter.
Although all languages in the .NET environment are initially compiled to a CPUindependent
language called Intermediate Language (which is analogous to Java byte
code), IL is not interpreted at runtime like Java. When code is initially executed, one
of several just-in-time (JIT) compilers translate the IL to native code on a method-bymethod
basis.
Cross-language integration is one of the major benefits provided by the CLR. If a colleague
has written a base class in C#, you can define a class in VB.NET that derives
from it. This is known as cross-language inheritance. Also, objects written in different
languages can easily interoperate. The two parts of the CLR that make this interoperation
possible are the Common Type System and the Common Language
Specification.
Common Type System
The Common Type System (CTS) defines rules that a language must adhere to in
order to participate in the .NET Framework. It also defines a set of common types
and operations that exist across most programming languages and specifies how
these types are used and managed within the CLR, how objects expose their functionality,
and how they interoperate. The CTS forms the foundation that enables
cross-language integration within .NET.
Common Language Specification
The Common Language Specification (CLS) is a subset of the CTS that describes the
basic qualities used by a wide variety of languages. Components that use only the
features of the CLS are said to be CLS-compliant. As a result, these components are
guaranteed to be accessible from any other programming language that targets .NET.
Because VB.NET is a CLS-compliant language, any class, object, or component that
you build will be available from any other CLS-compliant programming language in
.NET.
No comments:
Post a Comment