Saturday, December 30, 2023

Object Oriented Programming- 1

C# is an object oriented programming language.

The four basic principles of object-oriented programming are:

  • Abstraction Modeling the relevant attributes and interactions of entities as classes to define an abstract representation of a system.
  • Encapsulation Hiding the internal state and functionality of an object and only allowing access through a public set of functions.
  • Inheritance Ability to create new abstractions based on existing abstractions.
  • Polymorphism Ability to implement inherited properties or methods in different ways across multiple abstractions.

Note: Above content is from Microsoft Learning.

Let's try this with some practical examples.


Most of our programming are based on real world data called domain models. For examples, Order, Employee, Ticket, Transaction, Book etc.

Many other concepts we use are also can be considered like models. For example, Button, Page, Grid, Task etc.

All of these models have different properties (or attributes) and operations (or actions) associated with them.

We can define these models as Classes, which defines the properties and operations associated with them. This is called as Abstraction.

Example:

public class Order {
  public int OrderId {get; set;}
  public DateTime OrderDate {get; set;}
  public string Status {get; set;}
  public decimal TotalAmount {get; set;}
  public void Create(){
  }
  public void Ship(){
  }
  public void Complete(){
  }
}

Classes are blue prints of Objects

We can create as many as instance we want from a Class.

var o1 = new Order();
o1.OrderID = 12; var o2 = new Order();
o2.OrderID = 13;

Each and every object will have its own values for properties.

Behind the scenes, these objects are backed by Fields.

Fields are variables defined at class level. Each object created will have its own space for storing the values for fields.

Methods are operations defined within the classes, which will have set of statements to be executed when we invoke the method.

All these fields, methods and properties are called members of the class and can be restricted with access modifiers.

  • Members marked with public are accessible anywhere (from our program, or any program that refers our program).
  • Members marked with private are accessible within the class.

Note: There are few other modifiers, we can see them in detail separately.

The methods marked as private can be called from any methods defined within the class.

Thus we can expose only the properties and methods that should be accessed from outside using public access modifier and hide the internal operations and data using private access modifier

Example:

public class Order {
  public int OrderId {get; set;}
  public DateTime OrderDate {get; set;}
  public string Status {get; set;}
  public decimal TotalAmount {get; set;}
  public void Create(){
    // Do create order
    MarkStatus("Created");
    Notify();
  }
  public void Ship(){
    // Do ship order
    MarkStatus("Shipped");
    Notify();
  }
  public void Complete(){
    // Do complete order
    MarkStatus("Completed");
    Notify();
  }
  private void Notify(){
  }
  private void MarkStatus(string newStatus){
    this.Status = newStatus;
  }
}

In the above code, we have two methods Notify and MarkStatus as private.

These two methods are called from Create, Ship and Complete public methods. So when these methods called, these methods will call the MarkStatus and Notify methods internally.

Outsiders cannot invoke the MarkStatus and Notify methods as they are private.

This concept where we build an object with related members (fields, properties, methods and events) is called as Encapsulation.

No comments:

Post a Comment

Object Oriented Programming- 2- Inheritance

Inheritance is one of the fundamental attributes of object-oriented programming. It allows you to define a child class...