What is OOPS?

      What is  Object oriented programming  ?    

  • Object oriented programming is programming language.
  • It follows concept of  "Objects" which have some "Data Fields" and there are some procedures associated with it known as "Methods".
  • Objects are used to interact with other design applications and programs. These objects are instance of classes.
  • Object oriented programming are designed to overcome the shortcomings of procedural programming.
  • Using Object oriented programming , we can improve reliability and readability of our program.
  • There are some conventions of Object oriented programming. 
  1. Classes and objects
  2. Encapsulation
  3. Inheritance
  4. Polymorphism
  5. Abstraction
  • We will discuss these concepts of  Object oriented programming further.

      How does .NET support Object oriented programming?

  • The basic approach adopted by the framework is Object oriented and the framework would support only Object oriented code. 
  • So no more C and COBOL applications! Although C++ and OO-COBOL would work perfectly fine.

Classes and Objects


Classes in Object oriented programming
  •  A class is simply a representation of a type of object.
  • Class is composed of three things: a name, attributes, and operations.  
  • There was some major problems in earlier approach is that the data and the functions working upon the data being separated.
  • But the rule of Object oriented says that if the data and functions acting upon the data can go together let them be together, So we define a unit which contains data and its function together as a Class.
Class Example

 The Employee class would be like this

 /*------------------------------------------------------------*/

public class Employee // class name
{
    int EmployeeID;      // These are instance variables. They belong to object. 
    string EmployeeName; // These are attributes of a class at any point of time the value of these variables determines the state of the object.
    string EmployeeAddress;
    double EmployeeBasicSalary; 

    public void AddDetails()  // Methods or Member function. These determine the behavior of a class
    {
        // Logic to add the details of employee.
    }
    public void PrintDetails()
    {
        // Logic to print the details of employee.
    }
}

 /*------------------------------------------------------------*/

Objects in Object oriented programming
  • An object is a software bundle of variables and related methods.
  • Objects are related to real life such as people,cars and houses.
  • Objects are instances of classes
  • The object is declared in the below way
                   Employee emp1 = new Employee();
  • An object has following characteristics
  1. Attribute: The set of values of an attribute of a particular object is called its state. In Class Program attribute can be a string or it can be a integer or any other simple or complex Datatype.
  2. Behaviour: Every object has behavior. If a behavior of an object needs to be performed, then the corresponding method is called.
  3. Identity: Each time an object is created the object identity is been defined.This identity is usually created using an identifier which is derived from the type of item

Encapsulation


/*------------------------------------------------------------*/

class Employee
{
    /* Class containing variables. These are instance varibales */
    int employeeId;
    string employeeName;

    public string GetemployeeName()
    {
        return employeeName;
    }

    public void SetEmployeeName()
    {
        // Logic to Set Employee Name.
        // Busiess rules can be applied here.
    }

    public void setemployeeId()
    {
        // Logic to Set Employee ID.
        // Busiess rules can be applied here like employeeId can't be negative.
    }

    public int getemployeeId()
    {
        return employeeId;
    }
}

 /*------------------------------------------------------------*/

Inheritance


Polymorphism


Abstraction


Properties in C#

 There are three types of properties given in C#

1. Read/write (Both getter and setter)

  class Employee
    {
        int employeeID;
        public int EmployeeID
        {
            get { return employeeID; }
            set { employeeID = value; }
        }
    }


2. Read Only  (only getter)

  class Employee
    {
        int employeeID;
        public int EmployeeID
        {
            get { return employeeID; }
        }
    }


3. Write Only (Only Setter)

  class Employee
    {
        int employeeID;
        public int EmployeeID
        {
            get { return employeeID; }
            set { employeeID = value; }
        }
    }

Constructors

  •  Constructors are special member function, which are used initializing the class data members
  •  In Object oriented programming constructors are quite important since initialization of data members was not allowed at the time of declaration.
  • Constructors are the methods of class.
  • They are called as soon as the object is created. (Note that object is created and not the reference) The name of the constructors is same as the name of the class.
  • So In an encapsulated body (Class) if you will find a Method with the same name as that of class then it is a constructor and it is called as soon as the object is created.
  • C# has 4 types of constructors
 1. Default
2. Parameterized
3. Private
4. Static

Default InBuilt Constructor

public class Employee
    {
        private int employeeID;
        private string employeeName;
        private bool isEmployeed;

        /*No constructor specified therefore default constructor comes into picture which will initialize employeeID to 0 and employeeName = "" */

        public int printEmployeeID()
        {
            return employeeID;
        }
        public string printEmployeeName()
        {
            return employeeName;
        }
        public bool printisEmployeed()
        {
            return isEmployeed;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            int employeeID;
            string employeeName;
            bool isEmployeed;
            Employee emp1;

 // Here only reference is created so constructor will not be called.
            emp1 = new Employee();

 // Here the object is created so Default parameterless constrctor will be called.
            employeeID = emp1.printEmployeeID();
            employeeName = emp1.printEmployeeName();
            isEmployeed = emp1.printisEmployeed();
            Console.WriteLine("employeeID initiatlized by default constructor to " + employeeID);

//employeeID will be 0 which is initialized by default constructor
            Console.WriteLine("employeeName initiatlized by default constructor to " +employeeName);

// employeeName will be "" which is also initialized by default constructor.

            Console.WriteLine("isEmployeed initiatlized by default constructor to " + isEmployeed);

// isEmployeed will be false by default which is also initialized by default constructor.

            Console.Read();
        }
    }

Destructors


Sequence of calling of Constructors


        class Person
        {
          public Person()
            {
                Console.WriteLine("In Person Class constructor");
            }
        }
        class Employee : Person
        {
            public Employee()
            {
                Console.WriteLine("In Employee Class Constructor");
            }
        }

        class Program
        {
            static void Main(string[] args)
            {
                Person person1 = new Person(); // Only Person class constructor is called.
                Employee emp = new Employee(); // First Person class constructor is called then Employee class constructor is called.
                Person refobj = new Employee(); // First Person class constructor is called then Employee class constructor is called.
                Console.Read();
            }
        }

Sequence of calling of Destructors

Destructors cannot be defined in structs. They are only used with classes.
A class can only have one destructor.
Destructors cannot be inherited or overloaded.
Destructors cannot be called. They are invoked automatically.
A destructor does not take modifiers or have parameters.


        class Person
        {
          ~ Person()
          {
              Console.WriteLine("In Person Class Destructor");
          }
        }
        class Employee : Person
        {
            ~ Employee()
          {
              Console.WriteLine("In Employee Class Destructor");
          }
        }

        class Program
        {
            static void Main(string[] args)
            {
                Employee emp = new Employee(); // First Employee class destructor is called then Person class destructor is called.   
            }
        }

Use of this Keyword


this Keyword with instance variables

When we use the instance variables (those belong to object) then it is always recommended to write this before the variable.
It will avoid ambiguity for the compiler when local variables have same name as that of instance variables.

this keyword is used to represent the current instance.

Below example shows how the ambiguity is caused:


/*------------------------------------------------------------*/

public class Employee
    {
        private int employeeID;
        public int GetEmployeeID()
        {
            return employeeID;
        }
        public void setEmployeeID(int employeeID)
        {
            employeeID = 21;   // Refers local variable and not the class variable
        }
    }

 /*------------------------------------------------------------*/


Below program shows how the ambiguity is removed by this keyword.


/*-------------------------------------------------------------*/

public class Employee
{
    private int employeeID;
    public int GetEmployeeID()
    {
        return this.employeeID;
    }
    public void setEmployeeID(int employeeID)
    {
        this.employeeID = employeeID; // Refers instance variable and not the local variable.
    }
}

/*-------------------------------------------------------------*/


Hence it is always recommended to use this keyword when we are referring instance variables/Methods.

Note: this keyword can not be used with static variables and methods as they belong to class and not instance. for more details refer static section.


Use of static Keyword


Static Keyword can be used with
1. Classes
2. Members
3. Methods/Properties/Events

1. Classes

When a class is declared as static it can have only static members
It can not be instantiated (It is sealed class).

Example to illustrate static class

    static class JobLocation
    {
        public static string GetJobLocation() 
             { 
                   return "H.No. Abc Test Street"
             }
    }


2. Members
       Static variables belongs to class.
       Non Static members belong to instance i.e. object of a class.
       A class can have static as well as non Static members.
       When an instance of a class is created it will have a separate copy of non static Members but a single copy of static member which belongs to class and not to the instance of that class.
       It is not possible to use this keyword with static members.

    class Employee
    {
        int employeeID;
        static string companyName;
    }


3. Methods/Properties/Events
Static methods can only use static Members. 
A class can have static methods as well as non Static methods.

class Employee
    {
        int employeeID;
        static string companyName;
      
        public static void EmployeeData()
        {
            // we can access companyName here as it is static.
            // we can not access employeeID as it is non static.

            companyName = "Test Company";
        }

        public void EmployeeDetails()
        {
            EmployeeData();
            employeeID = 2;
        }

        public void PrintEmployeeDetails()
        {
            Console.WriteLine("Comapny Name is " + companyName);
            Console.WriteLine("EmployeeID is " + employeeID);

        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Employee emp = new Employee();
            emp.EmployeeDetails();
            emp.PrintEmployeeDetails();
            Console.Read();

        }
    }


And to directly call static method we use class and not the instance

    class Employee
    {
        int employeeID;
        static string companyName;
        public static void EmployeeData()
        {
            // we can access companyName here as it is static.
            // we can not access employeeID as it is non static.

            companyName = "Test Company";
        }
        public void EmployeeDetails()
        {
            employeeID = 2;
        }
        public void PrintEmployeeDetails()
        {
            Console.WriteLine("Comapny Name is " + companyName);
            Console.WriteLine("EmployeeID is " + employeeID);

        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Employee.EmployeeData();
            Employee emp = new Employee();
            emp.EmployeeDetails();
            emp.PrintEmployeeDetails();
            Console.Read();

        }
    }

4. Static Constructors

Static Constructors are used to initialize static Data.
Static Constructors can not have access specifier ( Not even private) and cannot be overloaded.

class Employee
    {
        int employeeID;
        static string companyName;
        static Employee()
        {
            // we can access companyName here as it is static.
            // we can not access employeeID as it is non static.
            Console.WriteLine("In Employee Class static constructor");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {

            Employee emp = new Employee();
            Console.Read();

        }
    }

Virtual Methods

Virtual Methods are the methods of the Base class which can be overridden by the Derived class.



    public class Person
    {
        public virtual void isOnPayroll()
        {
        // Logic to Check is the person is on payroll or not.
            Console.WriteLine("In Person Class");
        }
    }
    public class Employee : Person
    {
        public override void isOnPayroll()
        {
            // The method is overriden in the child class.
            // The Employee class can override the isOnPayroll or it can make it virtual for further derived class.
            Console.WriteLine("In Employee Class");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Person person1 = new Person();
            person1.isOnPayroll(); // In Person Class because the object is of person.
            Employee emp = new Employee();
            emp.isOnPayroll();      // In Employee Class overridden method is called.
            Person refobj = new Employee();
            refobj.isOnPayroll();  // In Employee Class. Reference of person class is used and object of Employee
                                    // is created.
            Console.Read();
        }
    }