5. Object Oriented Program I(OOPs)

Object Oriented Program (OOP) is a method of programming in which programs are organised as cooperative collections of objects. Each object is an instance of a class and each class belong to a hierarchy.


Classes and Objects

A class is a template or blue print used to create an object. A class can contain fields, methods, properties and indexers.

An object is an instance of a class. It can be considered a "thing" that can perform a set of related activities.

A field is a class member that holds a value in it. A field can be an instance, static, constant or read-only.

A method is a member that implements an action

Prooperties are smart fields. Properties contains get and set accessors.


Object Oriented Programming Language Core Concepts:

1. Abstraction: Abstraction reduces complexity by hiding irrelevant detail information. Object-oriented languages provide abstraction via classes.

2. Encapsulation: Encapsulation is combining members and member methods into a single logical unit in a class.

3. Inheritance:Inheritance is a mechanism to create a new class from an existing one. Also known as code reuse.

4. Polymorphism: means the ability to request that the same operations be performed by a wide range of different types of things.


Encapsulation and Abstraction:

Encapsulation and Abstraction: both concepts that go together in the object oriented approach. Abstraction hides the unncessary information and Encapsulation means that a group of related properties, methods, and other members are treated as a single unit or object.

        //Example Listing 5.1
        // Encapsulation example code
        using System;
        namespace QSSTraining
        {
            public class Student
            {
                private string _name;
                public void SetName(string name)
                {
                    _name = name;
                }
                public string GetName()
                {
                    return _name;
                }
            }
            public class Program
            {
                public static void Main(string[] args)
                {
                    Student oStudent = new Student();
                    oStudent.SetName("Alan Rees");
                    Console.WriteLine("Student Name: {0}", oStudent.GetName());
                }
            }
        }
        

Properties

Properties are named members of classes, structs, and interfaces. They provide a flexible mechanism to read, write the values through set and get accessors.

        //Example Listing 5.2
        // Set and Get properties example code
        using System;
        namespace QSSTraining
        {
            public class Course
            {
                private string _courseName;
                public string CourseName
                {
                    get { return _courseName; }
                    set { _courseName = value; }
                }
            }
            public class Program
            {
                public static void Main(string[] args)
                {
                    Course oCourse = new Course();
                    oCourse.CourseName = "CSharp";
                    Console.WriteLine(oCourse.CourseName);
                }
            }
        }
        Note:  The above Course class also can be written as below.
               Not necessary to declare set and get properites.
               They are implicit.
        public class Course
        {
            public string CourseName;
        }
        

Methods

A method defines a procedure that you can perform on an object or a class. C# .NET supports different methods. The instance method and static methods are described blow. The virtual, override and overloading methods are explained in Polymosphirm in next chapter.

Instance Method:

Instance methods operate on objects. In order to call an instance method, you need a reference to an instance of the class that defines the method.

        //Example Listing 5.3
        // Instance Method example code
        using System;
        namespace QSSTraining
        {
            public class Program
            {
                public void InstanceMethod()
                {
                    System.Console.WriteLine("Invoked Instance Metrhod on the object");
                }
                private static void Main(string[] args)
                {
                    Program objectProgram = new Program();
                    objectProgram.InstanceMethod();
                }
            }
        }
        
Static Method:

Static methods are called without an instance reference. Static methods operate on class rather on instances of the class. Static methods only have access to the static members of the class.

        //Example Listing 5.4
        //Static Method example code
        using System;
        namespace QSSTraining
        {
            public class Program
            {
                public static void StaticMethod()
                {
                    System.Console.WriteLine("Invoked Static Metrhod on the class");
                }
                private static void Main(string[] args)
                {
                    Program.StaticMethod();
                }
            }
        }
        

Interface

Interface is a contract that defines the signature of the functionality.
An interface is defined by the key word interface.
An interface has no implementation.
A class implementing an interface must provide the implementation of the interface members.
All the methods and properties defined in an interface are by default public and abstract.

        //Example Listing 5.5
        //Interface example code
        using System;
        namespace QSSTraining
        {
            interface ISetAccount
            {
                void SetBalance(double balance);
            }
            interface IGetAccount
            {
                double GetBalance();
            }
            class MyAccount : ISetAccount, IGetAccount
            {
                double _balance;
                public double GetBalance()
                {
                    return _balance;
                }
                public void SetBalance(double balance)
                {
                    _balance = balance;
                }
            }
            public class Program
            {
                public static void Main(string[] args)
                {
                    var oMyAccount = new MyAccount();
                    double balance = 100.00;
                    oMyAccount.SetBalance(balance);
                    Console.WriteLine("Balance = {0}", oMyAccount.GetBalance());
                }
            }
        }
        

Delegate

Delegate is a class that can hold a reference to a method or a function. A delegate is a type safe function pointer.

Single Cast Delegate

        //Example Listing 5.6
        //Single Cast Delegatge example code
        using System;
        namespace QSSTraining
        {
            public delegate void TestDelegate();
            public class Program
            {
                public static void DisplayMethod()
                {
                    Console.WriteLine("DisplayMethod called by delegate..");
                }
                public static void Main(string[] args)
                {
                    var oDelegate = new TestDelegate(DisplayMethod);
                    oDelegate();
                }
            }
        }
        

Multi Cast Delegate

        //Example Listing 5.7
        //Multi Cast Delegatge example code
        using System;
        namespace QSSTraining
        {
            public delegate void TestDelegate();
            public class Program
            {
                public static void DisplayMethod1()
                {
                    Console.WriteLine("Display Method 1");
                }
                public static void DisplayMethod2()
                {
                    Console.WriteLine("Display Method 2");
                }
                public static void Main(string[] args)
                {
                    var oDelegate = new TestDelegate(DisplayMethod1);
                    oDelegate += new TestDelegate(DisplayMethod2);
                    oDelegate();
                }
            }
        }
        

Garbage Collections

C# .NET supports the Garbage Collector and is part of the memory management system.. The purpose of Garbage Collection is to dispose the objects that are no longer needed, and freeing up the space.