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.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 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:
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, Gas Level,
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, Shift
Gears, or Turn. The methods represent
behaviors the object can execute. And events represent notifications. For
example, a Car object might receive an Engine Overheating 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 Go Forward method, a Go
Backward 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—the
entire 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 Sports
Car 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 I
Drivable interface (by convention, interfaces usually begin with I), which specifies the Go
Forward, Go Backward, 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 Sports
car. The Sport scar class might be the
base class for another derived class.
No comments:
Post a Comment
Thank you for your valuable comment. Stay tuned for Updates.