Object-Oriented Programming an introduction

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 can also use the Visual Studio programming environment to create your own classes to use in your programs.

Objects, Members, and Abstraction

An object is a programmatic construct that represents something. In the real world, we think of objects like 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

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 would be the class. The plans can be used to make any number of cars, and changes in 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 something interesting or noteworthy has happened.

To take a real-world example, consider a car as an object. 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 of noteworthy happen-stances. 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 only consist of a few properties, methods, and perhaps an event or two. More complex objects might require numerous properties and methods, and might even require 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 by itself, but it also consists of subordinate objects.

A Car object might contain, for example, 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 have different behavior 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 itself contain several SparkPlug objects.

Encapsulation

A principle of object-oriented programming is encapsulation. Encapsulation is the concept that the implementation of an object is independent from 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 the implementation of the interface has been 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 and should contain all of the functionality that works with that data. The internal data of an object should never be exposed in the interface; thus, fields should rarely be Public (public).

Let's return to the Car example: if a Car object interacts with a Driver object, the Car's interface might consist of a GoForward method, a GoBackward method, and a Stop method. This is all the information that the Driver would need to interact with the Car. The Car might contain an Engine object, for example, but the Driver doesn't need to know about that Engine object—all it 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 that is interacting with it, 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's public interface. I

f 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 would thus be able to interact with the Driver object.

Inheritance Polymorphism

Inheritance allows you to incorporate all of 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 all of 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 of 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.

Overloading allows you to create multiple members with the same name. Each member that shares a name must have a different signature. Overloading is most commonly used in methods, but C# allows you to overload operators as well. In this lesson, you will learn how to create overloaded members.

This method is perfectly acceptable on its own. But suppose you want to allow the client to designate a duration parameter for the method if needed? Or perhaps you would like the method to be able to accept an integer or a string as the DisplayValue parameter. Visual Basic .NET does allow you to designate optional parameters, but this functionality is unavailable to C# users. Furthermore, optional parameters do not address the second situation where a method needs to accept arguments that could be of multiple types.

The solution is to provide overloads. Overloads are multiple methods with the same name. Overloaded methods must have different signatures, but need not have the same return type nor access level. When an overloaded method is called, the run time examines the types of the arguments supplied in a method call. It then matches the argument list with the available overload signatures and calls the one that fits. If no overload fits the types of arguments that are supplied, an error results.

The most commonly overloaded type of member is member methods. You can create overloaded methods in both Visual Basic .NET and C#. C# allows you to overload operators, thereby providing custom operator functionality for user-defined types.

Creating Overloading Methods

You can create an overloaded method in the same way that you would create any other method: by declaring the method with a name, an access level, a return type, and an argument list. An overloaded method must have the same name as a preexisting method but must have a different signature. Access level and return type can be the same or different.

Two methods are defined, each with the same name but with different signatures and a separate implementation for each. When a method with the name DisplayMessage is called, the run time examines the type of the argument supplied to it. If a String is provided, the method that takes a String is called. If an Integer is provided, the method that takes an Integer is called.

To create an overloaded method

Declare a method that has the same name as an existing method. This method must have a signature that differs from any preexisting methods of the same name. The access level and return type can be the same or different from other methods of the same name.
Provide an implementation for the newly declared method.

Overloading Operators with Visual C#

When working with user-defined types, it might sometimes be convenient to define arithmetic, logical, or comparison operators that can operate with these types.

The type component of the syntax represents the type that this operator acts on and returns. Argument1 and Argument2 represent the arguments that the operator takes. For a unary operator, there will be only one argument, and it must be of the same type as type. For a binary operator, there will be two arguments, and at least one must be the same type as type. Op represents the operator itself, such as +, -, >, !=, and so on. An overloaded operator must be public, so clients working with your user-defined type can access it.

An overloaded operator must also be static. The definition for an overloaded operator must occur within the user-defined type that it is to act upon. Although the examples shown use structs, you can define overloaded operators for both structs and classes: there is no difference in the way they are defined.

related post

DAY 11 OOPS INTRODUCTION

DAY 12 POLYMORPHISM

DAY 13 INHERITANCE AND POLYMORPHISM

DAY 14 EBUGGING TOOLS IN DOT NET

DAY 15 DEBUG AND TRACE IN CLASSES

DAY 16 UNIT TEST PLAN

DAY 17 EXCEPTIONS IN VISUAL STUDIO

DAY 19 ADO.NET INTRODUCTION

DAY 20 DATA ACCESSING IN DOT NET

DAY 21 DATA BASE OBJECTS


No comments:

Post a Comment