Table of Contents

    Introduction to Object-Oriented Programming (OOP) Concepts in C#: Explained

    Introduction to Object-Oriented Programming (OOP) Concepts in C#: Explained

    Objective

    • Evolution of different programming approach
    • Introduction to Object-Oriented Programming Approach
    • Overview of Object-Oriented Programming
    • Characteristics of an Object

    Evolution of Programming Approaches

    As we have seen in the previous course, for automating the solution of a problem, we need to do programming. There were different programming approaches which evolved based on requirements and better programming practices. In the beginning, the programs will be small and simple. Later the programs will become more and more complex based on the requirements. So there is a need for better programming approaches.

    The following are the different programming approaches which evolved. • Unstructured Programming • Structured Programming • Procedural Programming • Modular Programming • Object-Oriented Programming

    Elements of Programming

    Data

    Programs are set of instructions that act upon data that is received (input) and transforms it into required information.

    Expressions

    Instructions that carry out required operations on the given data.

    Functions/Methods

    A block of instructions that accept some data, carry out some operations and returns an output.

    Structured Programming Approach

    Structured approach to programming, implements a system as a collection of functions that accepts data, processes it and returns an output. A function some times calls another function, when it wants to make use of the functionality defined in the other function.

    A program to calculate salary of an employee might look some what like this. A function to calculate salary, calling another two functions that calculates HRA and DA. The values returned by these modules is added up in the module that calculates salary,

    In Structured programming the flow of program execution is based on the structure of the program written. There is more dependency between the variables and the program like a chain.

    Example : C language

    Object Oriented Programming Approach

    We write programs that make up a software to solve a problem in real world. In real world problem domains we do not find any functions that work on data. We find objects that are described by some state (data) and having some behaviours (functions).

    Object Oriented Approach to programming is about implementing a system as collection of objects that have state and behaviours, interacting with each other to achieve expected functionalities.

    Example: C++ & Java

    In OO programming, the program basic entity is object. An object is any real world entity that has a well-defined structure and behaviour. Eg: Pencil, Laptop, Mobile, Car, Book, and Human. Every object has its own characteristics. Every object has 5 sets of characteristics.

    Characteristics of an object

    An object is a real-world entity that has a well-defined structure and behavior. It typically processes the following characteristics:
    • State
    • Behavior
    • Responsibility
    • Communication
    • Identity

    State

    State of an object is a set of properties that describe the object, which can take different values during the lifetime of the object.
    Eg: Bob is an employee of tech smart, let us have a look into Bob’s properties as an employee:

    Name: Bob
    Emp.No. 2312
    Department: Marketing
    BasicPay = 10,000
    Designation: Manager

    Linda is another employee
    Name: Linda
    Emp.No. 23612
    Department: Marketing
    BasicPay = 9,500
    Designation: Executive

    Knowing Responsibility: It is the responsibility of an object to know its state

    Behavior

    Behaviour is how objects react to their state changes or any other operations performed on it


    Bank account
    State:
    Account number: 121XXXXXXX
    Account Balance: Rs. XXXXX/-
    Opening Date: 12/01/2005
    Branch: XXXXX
    Bank account


    Behaviors
    Withdraw
    Deposit
    Generate Transaction Statement

    A bank account can perform these responsibilities, by making use of state values or other data provided to it.
    Doing Responsibility: It is the responsibility of the object to behave according to the operations performed on it.
    Responsibility
    Doing responsibilities
    Doing something itself, like creating an object/doing a calculation
    Initiating action in other objects
    Controlling and coordinating activities in other objects
    Knowing responsibilities
    Knowing about state
    Knowing about related objects
    Knowing about things it can derive or calculate
    Communication
    Objects can communicate with each other, provided they know each other.
    Objects can respond to the message sent by another object, by performing an action or returning some data.
    Identifying objects and behaviors
    The nouns in the requirements specification would suggest the Objects in the problem domain. The verbs from the requirements specification would suggest
    object behaviors.

    Object Oriented Programming in C#

    Object-oriented programming is a way of developing software applications using real-world terminologies to create entities that interact with one another using objects.

    Object-oriented programming makes applications flexible (easy to change or add new features), reusable, well-structured, and easy to debug and test.

    Most programming languages provide the following basic building blocks to build object-oriented applications:

    • Classes: A Class define the structure using methods and properties/fields that resemble real-world entity.
    • Methods: A method represents a particular behavior. It performs some action and might return information about an object, or update an object’s data.
    • Properties: Properties hold the data temporarily during the execution of an application.
    • Objects: Objects are instances of the class that holds different data in properties/fields and can interact with other objects.
    • Interfaces: An interface is a contract that defines the set of rules for a particular functionality. They are used effectively with classes using OOP principles like inheritance and polymorphism to make applications more flexible.

    Object-oriented Design Principles

    There are various object-oriented principles and techniques using which you can develop applications that are maintainable and extendable.

    The followings are four main principles of object-oriented programming:

    1. Abstraction
    2. Encapsulation
    3. Inheritance
    4. Polymorphism

    You will learn the above principles in detail in the next few pages.

    Steps for Developing Object-oriented Applications

    Developing an object-oriented application starts with the business requirement document. Typically, a business analyst provides you with a business requirement document after understanding and analyzing the requirement from the customer. So, the business requirement is the starting point.

    The followings are overall steps to develop an object-oriented application:

    1. Abstraction: First, identify essential entities and their characteristic from the business requirement for a high-level view.
      • Find nouns from the business requirement (the noun is the person, place, thing, or process).
      • Identify potential classes and their members from the nouns.
    2. Encapsulation: An implementation of abstraction in code. Create classes and their members with appropriate access modifiers to show functionalities and hide details and complexity.
    3. Define relationship: Establish relationships between classes using inheritance and polymorphism.
      • Inheritance
      • Polymorphism
    4. Use Principles & Patterns: Use the SOLID principles and Design Patterns as and when necessary to make applications flexible.
      • Single Responsibility Principle
      • Open/Closed Principle
      • Liskov Substitution Principle
      • Interface Segregation Principle
      • Dependency Inversion Principle

    Here you will learn about important object-oriented principles and patterns and when and how to use them in your application.