Return to site

Understanding The Core Object-Oriented Concepts In C#

C#---pronounced as C-sharp is a hybrid of C and C++, is a Microsoft programming language developed to compete with Sun's Java language. C# boasts type-safety, garbage collection, simplified type declarations, versioning and scalability support, and other features that make developing solutions faster and easier, especially for COM+ and Web services.

Let’s go ahead and look at some of the key concepts of object orientation applied in C# development services. We will also look at the basics of OOPS including Interfaces, Access Modifiers, inheritance, polymorphism etc.

Key Concepts of Object Orientation

  • Abstraction
  • Encapsulation
  • Polymorphism
  • Inheritance

What is Abstraction?
Abstraction can be defined as the ability to generalize a given object as a data type which has a specific set of characteristics and is able to perform a set of actions.

Object-oriented languages provide abstraction via classes. Classes define the properties and methods of an object type.

Example

You can create an abstraction of a dog with characteristics, such as color, height, and weight, and actions such as run and bite. The characteristics are called properties, and the actions are called methods.

A Recordset object is an abstract representation of a set of data.

Classes are blueprints for Object.

Objects are instances of classes.

Object References
When we work with an object we are using a reference to that object. On the other hand, when we are working with simple data types such as Integer, we are working with the actual value rather than a reference.

Creating a new object using the New keyword, we store a reference to that object in a variable. When we talk about early binding, it means that the code directly interacts with the object, by calling its methods directly. The compiler knows the object's data type ahead of time, hence it directly compiles the code to invoke methods on the object. Early binding also allows the IDE to use IntelliSense to aid our development efforts; it allows the compiler to ensure that we are referencing methods that do exist and that we are providing the proper parameter values.

On the other hand, late binding means that the code interacts with an object dynamically at run-time, providing a great deal of flexibility since the code has nothing to do with what type of object it is interacting with as long as the object supports the methods that you want to call.

Composition of an OBJECT
Whenever you use interface in order to get access to an object's data and behavior, the object's data and behaviors are contained within the object, so a client application can treat the object like a black box accessible only through its interface. This is what we call Encapsulation.


There are three main parts of Object:
1. Interface
2. Implementation or Behavior
3. Member or Instance variables

Composition of an OBJECT
Whenever you use interface in order to get access to an object's data and behavior, the object's data and behaviors are contained within the object, so a client application can treat the object like a black box accessible only through its interface. This is what we call Encapsulation.

Interface:- Interface can be defined as a set of methods (Sub and Function routines), properties (Property routines), events, and fields (variables or attributes) that are declared Public in scope.

Implementation or Behavior:- The code inside of a method is called the implementation. Sometimes it is also called behavior since it is this code that actually makes the object do useful work.

One thing to consider here is that encapsulation is a syntactic tool-allowing the code to continue to run without change.

Member or Instance Variables
The third important part of an object is its data, or state. When you look at it, each instance of a class is absolutely identical in terms of its interface and its implementation-the only thing that can vary at all is the data contained within that particular object.

These member variables are those which have been declared so that they are available to all code within our class. In most cases, member variables are Private in scope-available only to the code in our class itself.

It contains is definitions of events, indexers, methods and/or properties. The reason behind interfaces providing definitions is because of their inheritance by classes and structs, which must provide an implementation for each interface member defined. The interface pushes each component to disclose specific public members that will be used in a certain way.

Inheritance
You can call inheritance as the idea that one class, called a subclass, can be based on another class, called a base class. It serves as a mechanism for creating hierarchies of objects.

Normal base classes can be instantiated themselves, or inherited for that case. Derived classes can inherit base class members that are marked with protected or greater access. The derived class is engineered to provide better functionality, in addition to what its base class provides. Inheriting base class members in derived class is not mandatory.

Access Keywords:-

base -> Access the members of the base class.
this -> Refer to the current object for which a method is called.
 
Polymorphism
The derived ability to write one routine that can operate on objects from more than one class-treating different objects from different classes in exactly the same way can be termed as Polymorphism. To give you an example, if both Customer and Vendor objects have a Name property, and we can write a routine that calls the Name property regardless of whether we're using a Customer or Vendor object, then we have polymorphism.

To better understand this, take a vehicle as an example. A vehicle interface would only have those properties and methods that all vehicles have, a few of which might include paint color, number of doors, accelerator, and ignition. The same properties and methods would apply to all types of vehicles including cars, trucks, and semi-trucks.

When you apply Polymorphism, it will not implement code behind the vehicle's properties and methods. Instead, polymorphism is the implementation of the interface itself. If the car, truck, and semi truck all implement the same vehicle interface, then the client code for all three classes can be exactly the same.

C# provides polymorphism through inheritance and is vital during the project lifecycle while working together in a C#.net web development company.


Over to you
C# as a language is robust and matured. The more you learn about it, the more depth it will provide. Learning or upgrading on your current knowledge of C# is always a healthy choice. With dynamic interface and mass appeal, C# has a solid future.