find and solve || findandsolve.com
Please wait.....

Welcome back.






Before Read Terms of use

Object Oriented Programming Using C#

Object-Oriented Programming Concepts

Object oriented programmming(OOP) is a programming language where programs are organized around objest ato action and logic.
This is  the most popular programming languages in c# including abastraction,encapsulation,inheritance and polymorphism.

Object Oriented  has the following important features. 

Class

A calss is the core of nay mordern object oriented programming language.
OOP language compalsory  to create a class for representing data.
Class is a collection of method and variables.We can define a class using the class keyword in c#.
 

For Example

     class Artical
       {
         // something here
      } 

Object

Objest are the basic run-time entities of an object oriented programming system.
"An object is an instance of a class"
The classes are regarded as types for instances and instances can not change their type as runtime.

For Example

Syntax to create an object of class Artical

Artical objArical=new Artical();
All the programming languages supporting Object Oriented Programming will be supporting main concepts given below, 

 I . Encapsulation
 II .Inheritance
 III . Polymorphism 

Abstraction

"To represent the essential feature without representing the background details."
Abstraction and encapsulation are related features in object-oriented programming and it is used to hide the implementation details and display only essential features of the object.Abstraction is needed when we need to only inherit from a certain class,not need to instantiate objects of that class.
 

There are some important points about Abstract Class given below.
⦁    An abstract class is declared by abstract keyword.
⦁    Not allowed to create objects of the abstract class,or you cannot use the abstract clsss directly.
⦁    Abstract mehtods not allowed to declare outside the abstract class.
⦁    Not allowed to declare abstract class as Sealed Class.
⦁    If class declared Sealed,it cannot be inherited,abstract classes cannot be declared Sealed.
 

C# Abstraction Example

    class program
        {
            abstract class Parent
            {
                public abstract void Smile();
                public void Cry()
                {
                    Console.WriteLine("Child Can Cry");
                }
            }
            class Child : Parent
            {                 public override void Smile()                 {                     Console.WriteLine("Child Can Smile");                 }             }             static void Main(string[] args)             {                 Child objChild = new Child();                 Parent obj = objChild;                 obj.Smile();                 obj.Cry();                 public override void Smile()                 {                     Console.WriteLine("Child Can Smile");                 }             }             static void Main(string[] args)             {                 Child objChild = new Child();                 Parent obj = objChild;                 obj.Smile();                 obj.Cry();
            }         } 

Encapsulation 

Encapsulation is hide data.also behavior that are not necessary to its user.Encapsulation enables a group of properties, methods and other members to be considered a single unit or object.This is wrapping up of data under a single unit.This is the procedure of encapsulating data and functions into a single unit.It collects data members and member function into a single unit called class.

There are some important Encapsulation.

⦁    Encapsulation is also called "data-hiding" because encapsulation,the data in a class is hidden from

      other classes.
⦁    Hiding Type Members with Private Access.
⦁    Re-usability and easy to change with new requirements.
⦁    The code is very easy to test for unit testing.
⦁    Increased Flexibility because  the variables of the class as read-only or write-only depending on our project or requirement.
⦁    So readonly to make Get Accessor in the code. And write-only then make only use Set Accessor.
⦁    Your objects are able to manipulate the internal data point using a single named item.

C# Encapsulation Example

 namespace Example
       {
        class Employee         {             // Creating setter and getter for each property               public string EmployeeId { get; set; }             public string Name { get; set; }             public string Email { get; set; }         }     }
    using System;  
    namespace Example     {         class Program
       
        {             static void Main(string[] args)             {                 Employee emp = new Employee();                 // Setting values to the properties                   emp.EmployeeId = "120";                 emp.Name = "Devin";                 emp.Email = "devin@example.com";                 // getting values                   Console.WriteLine("EmployeeId = " + emp.EmployeeId);                 Console.WriteLine("Name = " + emp.Name);                 Console.WriteLine("Email = " + emp.Email);             }         }     }

Output:

EmployeeId= 120
Name = Devin
Email = devin@example.com
 

Inheritance 

Inheritance is one of the fundamental attributes of OOP.The most important or one of the primary concepts in object-oriented programming is inheritance.The most important or one of the primary concepts in object-oriented programming is inheritance,
This is use to inherit the properties from one class  to another class [one class is base class and antoher class is child class].That is,a class can inherit only from a single class.For example, "Salary" can inherit from type "StaffSalary", which inherits from type 
"StaffInfo", which inherits from the base class type "Emplyee". 
Because inheritance is transitive, the members of type "Emplyee" are available to type "Salary".
All members are not a base class are inherited by derived classes.The given below members are not inherited:

Static Constructors:A static constructor is used to initialize any static data.Needs to be performed once only. 

 class StaticExample
        {
            // initialized at run time.
 
            static readonly long toBaseline;
            // called at most one time, before any             static StaticExample()             {                 toBaseline = DateTime.Now.Ticks;             }         }

Static constructors have the following given  properties:
1.    Does not take access modifiers.
2.    A struct or class can only have one static constructor.
3.    Cannot be inherited or overloaded.
4.    A static constructor cannot be called directly.
5.    The static constructor is executed in the program,The user has no control on.
 

Instance constructors:

Finalizers:

This is also called destructors.

Remarks 
1.    cannot be defined in structs.
2.    They are only used with classes.
3.    A class can only have one finalizer.
4.    Cannot be inherited or overloaded.
5.     Invoked automatically.
6.    Does not take modifiers or have parameters.

For example, A declaration of a finalizer for the Vehicle class.
    class Vehicle
       {             ~Vehicle()  // finalizer             {                 // cleanup statements...             }         }             ~Vehicle()  // finalizer             {                 // cleanup statements...             }         } 

Polymorphism

This is combination of tow word "poly" another "morphs".Poly means MULTIPLE and morphs meeas FORMS,so polymorphism means MULTIPLE FORMS OR many forms.This is  a Greek word that means many forms.This is one of the core principles of OOP after encapsulation and inheritance.

Polymorphism Overview

It gains all the methods when a derived class inherits from a base class.
The derived class can choose whether to
⦁    override virtual members in the base class.
⦁    inherit the closest base class method without overriding it.
⦁    in non-virtual implementation,that hide the base class implementations.
 

There are two types of Polymorphism in c# given below.
⦁ Static / Compile Time Polymorphism.
⦁ Dynamic / Runtime Polymorphism. 

Static or Compile Time Polymorphism

The static or compile time polymorphism is achieved by method overloading and operator overloading in C#.It is also known as early binding.
Let's see a simple example of static or compile time polymorphism in C#.

using System;  
public class Staff {     public virtual void Salary()     {         Console.WriteLine("Get Salary...");     } }
public class Woman : Staff {     public override void Salary()     {         Console.WriteLine("Salary details...");     }
}
public class PolymorphismExample {     public static void Main()     {         Staff a = new Woman();         a.Salary();     } }

Output :

Salary details... 

Dynamic or  Runtime Polymorphism

The dynamic  or runtime polymorphism is a achieved by method overriding.also known as late binding.Let's see a another example of dynamic  or runtime polymorphism in C# where we are having two derived classes.

using System;
public class ExampleA {     public virtual void Display()     {         Console.WriteLine("Displaying A...");     } }
public class ExampleB : ExampleA {     public override void Display()     {         Console.WriteLine("Displaying B...");     }
}
public class ExampleC : ExampleA {     public override void Display()     {         Console.WriteLine("Displaying C...");     }
}
public class PolymorphismExample {     public static void Main()     {         ExampleA a;         a = new ExampleA();
        a.Display();
        a = new ExampleB();         a.Display();         a = new ExampleC();         a.Display();
    } }
Output:
Displaying  A...
Displaying  B...
Displaying  C...


Runtime Polymorphism with Data Members 

using System;  
public class Vehicle {     public string color = "Red";
}
public class Car : Vehicle {
    public string color = "White"; }
public class SealedExample {     public static void Main()     {         Vehicle v = new Car();         Console.WriteLine(v.color);
    } }
OutPut:
Red

Sundar  Neupane

Sundar Neupane

I like working on projects with a team that cares about creating beautiful and usable interfaces.

Comments



Report Response