C# OOPS

Object-oriented programming (OOP) is the approach used in the development of software. Before OOP there is a procedural oriented programming approach (POP). In POP programmers write functions to accomplish each task and the data is shared among these functions.

The limitation of POP is that it is hard to maintain the complexity of big projects and data security. To overcome these limitations OOP approach helps. In OOP, data is treated as a very important thing and does not allow it to move freely around the system.

Data is accessible to those functions which are tied to data. In this approach, the problem is decomposed into objects. It’s easier to maintain the complexity of a system based on objects. These objects can communicate with each other with the help of functions.

The OOP approach follows a bottom-up design approach. Another advantage of OOP is that one can add new data and functions whenever necessary.

The basic concepts of OOP are as below: 

  1. Class:

The class creates the user-defined data type. The class defines the data and behavior of the object. It is the blueprints for objects. Objects are the instances of these classes in OOPS.

For example, if we take the flower as a class, then rose, jasmine, lily etc. are the members of the class. Once you define a class, you can create any number of objects of this class.

The general syntax for defining a class is following: 

class <ClassName> 
{ 
    //Fields declaration 
    //Method definitions 
}

2. Object:

The object is the basic building block in OOP. Programming problem analyzed in terms of objects and the nature of communication between them.

Every object has data and functions to manipulate that  They are basic runtime entities and may represent a real world thing like a person, car etc. or user-defined data, such as lists, time etc.

The data represents the characteristics of the object that differentiate it from other objects of the same class. Like if we take an object of the class book, then, author, the number of pages, and the name of the

book are the characteristics of the objects.

Book
BookName

Author

Number Of Pages

ShowDetails()

The object interacts with each other by sending messages. Objects take place in memory. In C# objects are created using ‘new’ keyword as below

Type <obj_name> =new Type();

Example:

using System; 
namespace OOPDemo 
{
    class Book  //Defining class
    { 
        //Fields 
        public string BookName;  
        public string Author; 
        public int NoOfPages; 
       
       //Methods 
        public void ShowDetails() 
        { 
             Console.WriteLine("Book Name: {0}",BookName); 
             Console.WriteLine("Author: {0}", Author);
             Console.WriteLine("No. Of Pages: {0}", NoOfPages); 
        } 
    }
   class Program
   {
        static void Main(string[] args)
        { 
            Book b1 = new Book();   //Object creation using new operator 
            b1.BookName = "Software Architecture";
            b1.Author = "M. S. Murthy"; 
            b1.NoOfPages = 100; 
            Book b2 = new Book(); 
            b2.BookName = "Mathematics"; 
            b2.Author = "B. S. Swami"; 
            b2.NoOfPages = 200; 
            b1.ShowDetails();    
            b2.ShowDetails();
            Console.ReadLine(); 
        } 
    } 
}

The output of above program is:

Book Name: Software Architecture 
Author: M. S. Murthy 
No. Of Pages: 100 
Book Name: Mathematics  
Author: B. S. Swami 
No. Of Pages: 200

1. Data Abstraction: 

Abstraction means showing only essential information without adding unnecessary background details to the outside world.

For example, we use cell phones to call or for texting, but we don’t know how these functionalities implemented internally. This internal implementation is abstracted from users. In OOPS data abstraction reduces the complexity and ensures the efficiency.

2. Encapsulation: 

The process of combining data and function that operates on that data in a single unit (class) is known as encapsulation. In OOPs data and function are combined by defining a class.

Only the function within the class can have access to the fields in the class. This hiding of data from the outside world is known as data hiding. Encapsulation ensures the data security in OOPS. In C# encapsulation is achieved by using access modifiers.

3. Inheritance:

One of the most important reasons behind using OOPS approach is that it supports the idea of reusability. Inheritance is the process by which new class is created from the old class. In terms of OOPS newly created class is called as ‘derived class’ or ‘child class’ and the old class is known as ‘base class’ or ‘parent class’.

The derived class inherits the functionalities from a base class and one can also add new functionalities to the derived class. The base class defines the common attributes that are shared by derived classes.

Suppose vehicle is classier than two wheelers and four wheelers are the derived classes of it and they have inherited characteristics of the vehicle as well as they have some other specific characteristics also.

Vehicle
No of Wheels
Two Wheeler
No of Wheels (Inherited)
Other four wheeler specific attributes
Four Wheeler
No of Wheels (Inherited)
Other four wheeler specific attributes

The class in C# is inherited as below: 

class BaseClass 
{ 
   //Fields declaration 
   //Method definitions 
} 
class DerivedClass:BaseClass 
{ 
   //Fields declaration 
   //Method definitions 
}

Example:

using System; 
namespace InheritanceDemo 
{ 
   //Parent Class 
       class Parent 
       { 
           public void Show()
           {
               Console.WriteLine("This is parent class."); 
            } 
        } 
      //Child Class inherites Parent class
        class Child : Parent 
        { 
            public void Display() 
            { 
                Console.WriteLine("This is child class."); 
            }
        } 
       class Program
      {
          static void Main(string[] args)
         { 
            Child C = new Child();  //Creating object of child class 
            C.Show();               //Accessing Parent class method 
            C.Display();            //Accessing Child class method 
            Console.ReadLine(); 
        } 
    } 
}

The output of above program is: 

This is parent class. 
 This is child class.

4. Polymorphism:

Another important feature of OOPS is a polymorphism. Polymorphism is the ability to act differently in different situations. For example addition of two numbers generates the third number, but the addition of two string results in the third string which is the concatenation of two strings. In this case, we can use the single function name and depend upon the inputs the appropriate function is called.

Addition
Add()
Number
Add(numbers)
String
Add(strings)

Result String

Result Number

[Text Wrapping Break]

There are two types of polymorphism; first is static or compile time and second is dynamic or runtime polymorphism. In static polymorphism, the operation to be done is decided at compile time of the program.

In dynamic polymorphism, the operation to be done is decided at runtime. Function overloading, function overriding and operator overloading are the examples of polymorphism.

Advantages of OOPs:

The software complexity is managed as the system is divided into objects. So it is easy to maintain big systems in OOPS.

  1. In OOPs we can reuse code to a great extent.
  2. It is easy to partition work based on objects.
  3. The old system can be upgraded to new system easily.
  4. With the help of data, hiding programmer can build a secure program.
Content Protection by DMCA.com
Recommended Post   C# Case Control Structure