Introduction to Object Oriented Programming Concepts in C#

 Admin   
193 View    Jan 20 2020 11:05PM

Object Oriented Programming (OOP) is a programming model where programs are organized around objects and data rather than action and logic.

OOP allows decomposition of a problem 
into a number of entities called objects and then builds data and functions around these objects. 

  1. The software is divided into a number of small units called objects. The data and functions are built around these objects.
  2. The data of the objects can be accessed only by the functions associated with that object.
  3. The functions of one object can access the functions of another object.

 

 

 

OOP has the following important features.

Class

A class is the core of any modern Object Oriented Programming language such as C#.

In OOP languages it is mandatory to create a class for representing data.

A class is a blueprint of an object that contains variables for storing data and functions to perform operations on the data.

A class will not occupy any memory space and hence it is only a logical representation of data.

To create a class, you simply use the keyword "class" followed by the class name:

C#

class SampleClass

{

}

Object

 

 

 


Objects are the basic run-time entities of an object oriented system. They may represent a person, a place or any item that the program must handle.

"An object is a software bundle of related variable and methods."

"An object is an instance of a class"

Syntax to create an object of class Employee: 

Employee obj = new Employee();

 

 

Here’s another example:

Class Example
 
{
 
/* fields,
 
Variables,
 
Methods,
 
Properties,
 
*/
 
}

 

 

 

 

 

All the programming languages supporting Object Oriented Programming will be supporting these three main concepts, 

  1. Encapsulation
  2. Inheritance
  3. Polymorphism

 

Abstraction

Abstraction is "To represent the essential feature without representing the background details."

Abstraction lets you focus on what the object does instead of how it does it.

Abstraction provides you a generalized view of your classes or objects by providing relevant information.

Abstraction is the process of hiding the working style of an object, and showing the information of an object in an understandable manner.

 

Encapsulation

Wrapping up a data member and a method together into a single unit (in other words class) is called Encapsulation.

Encapsulation is like enclosing in a capsule. That is enclosing the related operations and data related to an object into that object.

Encapsulation is like your bag in which you can keep your pen, book etcetera. It means this is the property of encapsulating members and functions.

 

       class Bag

        {

            book;  

            pen;  

            ReadBook();

 

        }

 

 

 

Inheritance

When a class includes a property of a nother class it is known as inheritance.

Inheritance is a process of object reusability.

For example, a child includes the properties of its parents.

public class ParentClass

                                {

                                                public ParentClass()

                                                {

                                                                Console.WriteLine("Parent Constructor.");

                                                }

                                                public void print()

                                                {

                                                                Console.WriteLine("I'm a Parent Class.");

                                                }

                                }

                                public class ChildClass : ParentClass

                                {

                                                public ChildClass()

                                                {

                                                                Console.WriteLine("Child Constructor.");

                                                }

                                                public static void Main()

                                                {

                                                                ChildClass child = new ChildClass();

                                                                child.print();

                                                }

                                }

Output

Parent Constructor.
Child Constructor.
I'm a Parent Class.

 

Polymorphism

Polymorphism means one name, many forms.

One function behaves in different forms.

In other words, "Many forms of a single object is called Polymorphism."

Your mobile phone, one name but many forms:

Example:

 

  • As phone
  • As camera
  • As mp3 player
  • As radio