Wednesday, February 28, 2007

Object Oriented Concepts in C#

In this article we will discuss key concepts of object orientation with their practical implementation in C#. We will discuss here basics of OOPS including Interfaces, Access Modifiers, inheritance, polymorphism etc. This is my second article on

Thursday, February 22, 2007

OOP Concepts in .NET Useful Links

C# and VB Object-Oriented Programming in Visual Studio .NET

Object Oriented Terminology

OO Terminology

Encapsulation means that an object can hide its internal data structures from consumers of the object. Therefore, all of the object's internal data is manipulated through members (methods, properties, events, fields) of the object, rather than through direct references.
The primary benefits of encapsulation are maintainability and reusability. Code that takes advantage of encapsulation is more maintainable because consumers of the code work with the object through its public members. With a fully encapsulated object, for example, code outside the object cannot directly change a variable declared inside the object. By shutting off this direct access, fewer bugs are introduced because consumers of the object cannot inadvertently change the state of an object at run-time.
Abstracting the internal data of the object from consumers also leads to greater reusability. This follows because encapsulation leads to fewer dependencies between the consumer and the class and fewer dependencies is a prerequisite for creating reusable software.

The second characteristic of OO systems is polymorphism. This concept is defined as the ability to write code that treats objects as if they were the same when in fact they are different. In other words, polymorphism allows you to write code that is generic across a set of objects that provide the same public members. Underneath the covers, each object might be implemented differently. However, as far as the consumer is concerned, each object looks the same and can be treated as such. In VB.NET, polymorphism can be created using both classes and interfaces.
The benefits of polymorphism revolve around the central fact that consumers of objects do not have to be aware of how the object performs its work, only that it does so through a specific set of members. This makes writing code that uses objects simpler by allowing the code to treat the object as if it were a black box, which leads to increased maintainability. Along the same lines, polymorphism allows you to write less code because each individual object does not have to be dealt with separately. Finally, polymorphism lends itself to writing code that can be reused because it will not be specific to a particular object.

The final OO concept is inheritance. Inheritance allows objects to share their interfaces (the definition of their members) and/or implementation in a hierarchy. For example, Tyrannosaurus and Velociraptor objects might be derived or inherited from a more generic Theropod object. All three objects share a basic set of members and, possibly, behaviors, such as carnivorousness, although the descendant objects might also include additional members or override members of Theropod. Inheritance allows objects to become more specific further down the hierarchy by adding additional members. In a nutshell, inheritance allows objects to reuse features (either their definition or their code) of other objects to which they are naturally related. The primary benefit of inheritance is, thus, reuse.

Obviously, inheritance and polymorphism are closely related, and, in fact, inheritance is what makes polymorphism possible in OO designs. It is always the case that objects that are in an inheritance relationship can be treated polymorphically. For example, if the Velociraptor object is inherited from the Theropod object, any consumer that is designed to work with Theropod objects will also work with Velociraptor objects.

VB.NET developers can benefit from inheritance in two ways: through interface inheritance and implementation inheritance. Interface inheritance allows only the definition of the object to be reused, whereas implementation inheritance allows the actual code written for the ancestor object (and its ancestors all the way down the line) to be reused. Which one to use is a design decision as mentioned in Chapter 1 and discussed more fully in this chapter.

NOTE: If developers wanted to use a form of implementation inheritance in previous versions of VB, they had to design classes to take advantage of the concepts of containment and delegation. Basically, these concepts mean that a class accessible by a consumer will contain a private instance of a second class and delegate its services to the consumer through its own interface. Containment and delegation are familiar terms (along with aggregation) to COM programmers.

More OO Terms
Familiarity with the following OO terms will be useful in this discussion.
Class—The fundamental unit of code reuse. Implemented with the Class statement in VB.NET. Classes can be inherited from and nested, and can contain members.

Base class—A class that serves as the ancestor for an inherited class, although this usually means the direct ancestor. VB.NET uses the MyBase keyword to refer to the direct ancestor. As a group, the ancestors are referred to as the base classes. The base class at the top of the hierarchy is referred to as the concrete base class.

Abstract base class—A base class that cannot be instantiated at run-time and serves only as the template for derived classes. In VB.NET, this is accomplished using the MustInherit keyword.

Interface—A well-defined collection of members along with their signatures. Interfaces provide no implementation. They are created in VB.NET with the Interface keyword.
Members—The methods, properties, events, and fields that make up an interface or class definition.

Methods—Named blocks of code within a class that can accept arguments and might return a value. VB.NET supports both Sub methods that do not return a value and Function methods that do.

Properties—Data exposed by a class and manipulated through code that runs when the property is set to a value or retrieved. In VB.NET, property creation is unified in a single Property statement.

Fields—Data exposed by a class directly to the consumer. A field represents a data location and therefore, its access is not abstracted. Fields are created in VB.NET using public variables within a class.

Events—Notifications fired by a class and captured by a consumer. In VB.NET, the Event and RaiseEvent statements are used to declare and raise events in addition to the use of delegates.

Overloading—There are two forms of overloading. The first is that a class can contain multiple definitions of the same method, each with different arguments (signatures). The consumer can then choose which method to call. The second is that derived classes can overload methods in a base class to alter the signature of the method. VB.NET uses the concepts of optional parameters, parameter arrays (paramarrays), and the Overloads keyword to implement both forms.

Overriding—Overriding a method means that a derived class can implement its own functionality for a method or property. VB.NET uses the Overrides, Overridable, NotOverridable, and MustOverride keywords to specify how methods and properties might or might not be overridden.

Static member—A member that is exposed by the base class, is not allowed to be overridden in a descendant, is available to all instances of the class, and is available even before the instance is created. This is also referred to as a shared member and is implemented with the Shared keyword.

Virtual member—The opposite of a static member, also referred to as an instance member. This refers to a method that can be overridden in a descendant class. By default, all methods and properties in VB.NET can be made virtual by including the Overridable keyword.