The Object-Oriented Programming in C#
Published: 2020-11-26 14:23:48

The Object-Oriented Programming in C#


The Object-Oriented Programming (OOPs) in C# is a design approach where we think in terms of real-world objects rather than functions or methods. Unlike procedural programming language, here in oops, programs are organized around objects and data rather than action and logic.

OOPs, provide 4 principles. They are

    Encapsulation

    Inheritance

    Polymorphism

    Abstraction


We will discuss all these principles in detail using some real-time examples.
But first, we define some simple classes.

Class and Objects from Layman Point of View.
From the layman point of view, we can define a class as a blueprint of a specific object. Every living and non-living thing is considered as objects such as Car, People, Place, etc. Again, each and every object has some color, shape, properties, and functionalities.

For example, consider the luxury car Ferrari. Here, Ferrari is an object of the luxury car type. The luxury car is a class that specifies some characteristics such as speed, color, shape, etc. So any car manufactures company that makes a car and if that car meets all those requirements, then it is an object of the luxury car type.

If you take the example BMW, Lamborghini, and Cadillac, then all these cars are an object of the ‘Luxury Car’ class. Here, ‘Luxury Car’ is a class and every single car (BMW, Lamborghini, and Cadillac) is an object of the luxury car class. Now, let us understand what exactly class and objects from the programming point of view.

Class and Objects from Programming Language Point of View.
Here we are going to understand the class and objects from the C# programming language point of view. But this is also applicable to any object-oriented programming language like java and c++.

Class:
A class is simply a user-defined data type that represents both state and behavior. The state represents the properties and behavior is the action that objects can perform.

In other words, we can say that a class is the blueprint/plan/template that describes the details of an object. A class is a blueprint from which the individual objects are created. In C#, a Class is composed of three things i.e. a name, attributes, and operations.

Objects:
It is an instance of a class. A class is brought live by creating objects. An object can be considered as a thing that can perform activities. The set of activities that the object performs defines the object’s behavior.

All the members of a class can be accessed through the object. To access the class members, we need to use the dot (.) operator. The dot operator links the name of an object with the name of a member of a class.



using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;



namespace ClassDemo

{

    class Program

    {

        static void Main(string[] args)

        {



            Console.WriteLine("Example of defining a class in C#, setting its properties and calling its method/s.");



            // defining the first object of student class as tim

            Student tim = new Student();

            // setting the poblic attributes for the first object

            tim.FirstName = "Tim";

            tim.LastName = "Timmabc";





            // defining the second object of student class as jim

            Student jim = new Student();

            // setting the poblic attributes for the second object

            jim.FirstName = "Jim";

            jim.LastName = "Jimmxyz";



            //Calling the Display method for both objects

            tim.Display();

            jim.Display();



            Console.ReadKey();

        }

    }



    class Student

    {

        //Public properties

        public string FirstName { get; set; }

        public string LastName { get; set; }



        //Default constructor.

        public Student()

        {

        }



        //Public method

        public void Display()

        {

            Console.WriteLine("\t Student first name:  " + this.FirstName);

            Console.WriteLine("\t Student last name:  " + this.LastName);

        }
  }