Basic OOP(object oriented programing) concept in C# (CSharp).

Object Oriented Programming (OOP) is a programming model where programs are organized around objects and data rather than action and logic. Object Oriented Programing (OOP CSharp) allows decomposition of a problem into a number of entities called objects and then builds data and functions around these objects. Object Oriented Programming (OOP) uses a different set of programming languages than old procedural programming languages (C, Pascal, etc.). Here we will learn oops concepts with real time examples in C# programming language. Now let’s start learning.

oop-csharp-tech-diary

oop-csharp-tech-diary

 

These are a few principle concepts that form the foundation of object-oriented programming (OOP) in C#:

Class:

A class is user-defined ‘data type’ that contains data members, methods applied on that data, properties, indexers and nested type. In OOP languages it is mandatory to create a class for representing data. Actually, classes are used to create Objects. In blueprint analogy, a class is a blueprint and an object is a building made from that blueprint. Classes are declared using the keyword class.

Public class ClassName
{
// Methods, properties, fields, events, delegates
// and nested classes go here.
}

Object:

An object is an instance of a class. In general word, an object can be considered as a “thing” that implements set of related activities. The set of activities that the object perform is defined in the object’s methods. For example, Student (object) can read books, write exam etc. ‘new’ keyword is required to create objects of a class.

ClassName objectName = new ClassName();

In the above example we can see how to create object of a class.

Encapsulation:

Encapsulation is a mechanism of binding the data member & member function into a single unit. Oop Class is the best example of encapsulation. The class is kind of a container or capsule or a cell, which encapsulate the set of methods, attribute and properties to provide its indented functionalities to other classes.

Inheritance:

According to Microsoft “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 C# implicitly inherit from the Object class that supports dotNet class hierarchy and provides low-level services to all classes.”
Note:C# doesn’t support multiples inheritance, i.e. you can specify only one base class for a derived class.

Polymorphism:

Polymorphism means one thing in many forms. Basically, polymorphism is capabilities of an object to behave in multiple ways. For example, A man role changes at home, college, and outside the home. There are following types of polymorphism:

  1. Static polymorphism(compile time) – It is achieved using function overloading and operator overloading.
  2. Dynamic polymorphism(runtime time) – It is achieved using function overriding means using virtual functions.
Abstraction:

Abstraction is the process of hiding the complexity of an object, and showing the information of an object in an understandable manner. Abstraction lets you focus on what the object does instead of how it does it.

Note- When derived class inherited with abstract class; derived class must override abstract class methods.

using System;
using System.Collections.Generic;
using System.Linq;

namespace abstarction

{
    public abstract class university
    {
        public abstract void BTech();
        public abstract void MBA();
    }

    public class GBTU : university
    {
        public override void BTech()
        {
            Console.WriteLine("GBTU BTech Fee 50000/-");
        }

        public override void MBA()
        {
            Console.WriteLine("GBTU MBA Fee 100000/-");
        }
    }

    public class MTU : university
    {
        public override void BTech()
        {
            Console.WriteLine("MTU BTech Fee 40000/-");
        }

        public override void MBA()
        {
            Console.WriteLine("MTU MBA Fee 800000/-");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            GBTU g = new GBTU();
            g.BTech();
            g.MBA();
            MTU m = new MTU();
            m.BTech();
            m.MBA();
            Console.ReadLine();
        }
    }

}

 

Explanation:

  1. From above example, we have makes one abstract class university and two abstract methods Btech and MBA. GBTU andMTU both are override university course fee.
  2. University course common for both GBTU and MTU so university method BTech and MBA is abstract.
  3. GBTU and MTU inherited abstract method so university method must override here.

 

S Sharif

Founder of Technical Diary. Software engineer and passionate about technologies to learn and share. Crazy about best practices and new technology. I have worked in C#,ASP.NET MVC, HTML5, AngularJs SharePoint and SQL Server. I always try to keep myself updated and learn and implement new technologies. You can connect me on Twitter Facebook LinkedIn.

You may also like...

1 Response

  1. February 23, 2017

    […] Read Also: Basic OOP(object oriented programming) concept in C# (CSharp). […]

Leave a Reply

Your email address will not be published. Required fields are marked *