[OOPs] Class and object in c# with real time example

In this tutorial, I am going to discuss the "real-time example of Class and object in c#". To understand object and class concepts read this post carefully. In this tutorial, we will also learn the oops concepts in c# with real time examples
$ads={1}

What is OOPS..?
Object-oriented programming (OOP) is a programming structure where programs are managed by objects, classes and it’s action and logic. C# provides full support for 
object-oriented programming including encapsulation, inheritance, and polymorphism.
Class:

Class and object in c# with real time example

Class and object in c# with real time example

If we talking about the class in any language, a class is just a user define data type used to represent the relation between the data and functions or methods.

A class can have many things like variable, function, properties. In another way, we can say that class is a block of code where we can define the state and behaviour of objects, function, and variables.
$ads={2}

Note: 


By default class is internal.
Member of the class will be private


See below Syntax Format:

Class class name
{
      Statements;
     ----------------
    ------------------
}

Here I am taking an example of c# class.

using System;   //namespace
class Employee
{
   //variables
    public string name;
    int age;
    string address;

    public string departmant()           //method define by me
    {
      return "I.T.";
    }
    static void Main(string[] args)      //Main method
     {
      Employee emp = new Employee();   //Object creation of Employee class
      emp.name = "Jhon";
      emp.age = 10;
      emp.address = "Delhi";
      emp.departmant();
      Console.WriteLine(emp.name+" "+emp.age+" "+emp.address+" "+emp.departmant());
      }                                                                             
}


$ads={2}

In the above example, we can see that our method and variable are exits in class.

Object: 
An object is basically an instance of the class. It allocated the memory to access the member of the class. An object is a real-time entity.
 If we want to access the class member we need to create an object of that particular class.
In the below example, we take ‘Employee’ as a class then ‘emp’ are the object.

Syntex for Object:

ClassName objectname=new ClassName();

E.g.

Employee emp = new Employee();   //Object creation of Employee class
In the above syntax, the ‘new’ operator is used to create an object of a class. When we create an object, then the system creates the memory for data members and methods that are present in the class.

Important Point to remember 
An object is the base type of all classes in the dot net framework. This object can hold any kind of value, however, boxing will be done implicity and unboxing need type casting mechanism.


Abstraction and encapsulation


Abstraction
Basically, Abstraction is a process of hiding the unnecessary things and it provides operation part to end-user. So if we want to understand the abstraction we have to go for a real-life example.

Suppose that there is a cell phone. Actually, we all don't know how it internally works but we can use it by touching its screen, it means we know only about operational part i.e abstraction.


Encapsulation
Encapsulation is the process of protecting the information of an object from the end-user. Hide the data for security such as making the variables private, and expose the property to access the private data which would be public.

It means if there is abstraction there will be encapsulation, but it is not necessary that if there is encapsulation then there also will be an abstraction.


Also Read: 
                 Boxing and Unboxing With Simple Example
                 Lockdown SQL Course




2 Comments

  1. this is one of best concept that differ from other blogs and site.....

    ReplyDelete
  2. Highly Great solutions sir! thanku and keep posting

    ReplyDelete

Post a Comment

Post a Comment

Previous Post Next Post