Overview of Object-Oriented Programming

Visual Studio

Introduction

Let’s talk about programming using Visual Studio. All managed languages in the .NET Framework, such as Visual Basic and C#, provide full support for object-oriented programming including encapsulation, inheritance, and polymorphism.

  • Encapsulation means that a group of related properties, methods, and other members are treated as a single unit or object.
  • Inheritance describes the ability to create new classes based on an existing class.
  • Polymorphism means that you can have multiple classes that can be used interchangeably, even though each class implements the same properties or methods in different ways.

Classes and Objects

The terms class and object are sometimes used interchangeably, but in fact the term class describes the type of objects, while objects are usable instances of classes. The act of creating an object is called instantiation. Using a simple building blueprint analogy, a class is a blueprint and an object is the building made from that blueprint.

Each class can have different class members that include properties that describe class data, methods that define class behavior, and events that provide communication between different classes and objects. Fields and properties represent information that an object contains. Fields are like variables because they can be read or set directly. Properties have get and set procedures, which provide more control on how values are set or returned.

Both C# and Visual Basic allow you either to create a private field for storing the property value or use auto-implemented properties that create this field automatically behind the scenes and provide the basic logic for the property procedures.

Example Class with Property:

Class SampleClass
    Private m_Sample As String
    Public Property Sample() As String
        Get
            ' Return the value stored in the field.
            Return m_Sample
        End Get
        Set(ByVal Value As String)
            ' Store the value in the field.
            m_Sample = Value
        End Set
    End Property
End Class

Most properties have methods or procedures to both set and get the property value. However, you can create read-only or write-only properties to restrict them from being modified or read. In Visual Basic you can use ReadOnly and WriteOnly keywords. In C#, you can omit the get or set property method. However, in both Visual Basic and C#, auto-implemented properties cannot be read-only or write-only.

Methods

method is an action that an object can perform.In Visual Basic, there are two ways to create a method: the Sub statement is used if the method does not return a value; the Function statement is used if a method returns a value. A class can have several implementations, or overloads, of the same method that differ in the number of parameters or parameter types.

In most cases you declare a method within a class definition. However, both Visual Basic and C# also support extension methods that allow you to add methods to an existing class outside the actual definition of the class.

Constructors

Constructors are class methods that are executed automatically when an object of a given type is created. Constructors usually initialize the data members of the new object. A constructor can run only once when a class is created. Furthermore, the code in the constructor always runs before any other code in a class. However, you can create multiple constructor overloads in the same way as for any other method.

Class SampleClass
    Sub New(ByVal s As String)
        // Add code here.
    End Sub
End Class

Destructors

Destructors are used to destruct instances of classes. In the .NET Framework, the garbage collector automatically manages the allocation and release of memory for the managed objects in your application. However, you may still need destructors to clean up any unmanaged resources that your application creates. There can be only one destructor for a class.

Events

Events enable a class or object to notify other classes or objects when something of interest occurs. The class that sends (or raises) the event is called the publisher and the classes that receive (or handle) the event are called subscribers.

Inheritance

Inheritance enables you to create a new class that reuses, extends, and modifies the behavior that is defined in another class. The class whose members are inherited is called the base class, and the class that inherits those members is called the derived class. However, all classes in both C# and Visual Basic implicitly inherit from the Object class that supports .NET class hierarchy and provides low-level services to all classes.

Example:

Class DerivedClass
    Inherits BaseClass
End Class

By default all classes can be inherited. However, you can specify whether a class must not be used as a base class, or create a class that can be used as a base class only.

Summary

The purpose of this article was to just give you some basic terms and a brief introduction to the subject of Object-Oriented Programming. Object-oriented languages have a small set of interoperating principles. Break any one of them in creating a language and many of the benefits of object technology evaporate. If you are interested, you can read more on the subject here.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s