Wednesday, November 16, 2011

Object oriented programming (OOP)

Namespace
A name space is a logical grouping of classes and other data structures.
Ex. Animals
namespace animals
{
}

Class
A class is definition of a real life object.
Ex. Dogs
class dogs
{
public int AgeOfDog = 30;

public virtual void bark()
{
Console.WriteLine("Dogs bark.");
}
}

Object
An object is an instance of a class.
Ex. Jimmy
dogs jimmy = new dogs();

Access modifiers
Private A member that can be accessible only in the class where it declared.
Protected A member that can be accessible in same class and derived class.
Public A member that can be accessible any where, normally by using object of the class.
Internal A member that can be accessible inside the assembly, also called friend or Assembly member.

Inheritance
Inheritance is a technique that allows a class to acquire attributes of its base type.
Its an ability to create new classes based on existing classes. Inheritance referred as second pillar of OOPs.   

A derived type can access all the protected, internal(if derive type in same assembly) and public members of base type.
It can overload a method of base type, can redefine (override) a virtual method of base type

class dabaurman : dogs //dabaurman inheriting dogs
{
AgeOfDog = 12;
public override void bark() //Overriding bark function of dogs class
{
Console.WriteLine("Daburmans looks ready to bark always, max age of them is " + AgeOfDog.Tostring());
}
}

Abstraction (Data hiding)
Abstraction means to show only the necessary details to the client of the object. Do you know the inner details of the Monitor of your PC? What happen when you switch ON Monitor? Does this matter to you what is happening inside the Monitor? No Right, Important thing for you is weather Monitor is ON or NOT.
Let’s say you have a method "CalculateSalary" in your Employee class, which takes EmployeeId as parameter and returns the salary of the employee for the current month as an integer value. Now if someone wants to use that method. He does not need to care about how Employee object calculates the salary? An only thing he needs to be concern is name of the method, its input parameters and format of resulting member, Right?
We use thousands of methods provided by .net library without having knowledge of internal process of methods, like Math.Round(decimal d), how this function rounds the number we don't know but we can use it, this is an example of Abstraction.

Encapsulation (Data binding)
Encapsulation is process of binding data with code in a single entity. This keeps the data safe from outside interface and misuse.
Encapsulation is a protective wrapper that prevent data from being arbitrary accessed by other code.
Encapsulation referred as first pillar of OOPs.  
Variables of a class can hold data, we can give access to outside code over this type of members according us, like read only, write only or read write.

Accidental modification can happen to the public variables.
public int AgeOfDog = 30;
Putting data with related functions inside class is called Encapsulation.
Encapsulation is a process of binding or wrapping the data and the codes that operates on the data into a single entity. This keeps the data safe from outside interface and misuse. one way to think about encapsulation is as a protective wrapper that prevents code and data from being arbitrarily accessed by other code defined outside the wrapper.

Now question is that How Encapsulation hide data? 

Class Customer
{
    private int _age; //private member

    public int getAge(){return _age;}
    //OR 
    //public int Age
    // {
    //    get{return _age;}
    //    set{_age = value;}         
    // }
}
1. We can have read-only, write-only or read-write access to a member(_age) by having get, set or get and set both respectively .
2. We can validate data before setting value(in property or method).
3. We can audit or log data before setting value(in property or method).

Polymorphism
Its a technique in that a object can take multiple forms.
A base type variable can hold object of any of its drive type. Polymorphism referred as third pillar of OOPs.    

Types of Polymorphism -
1. Static Polymorphism(Compile time Polymorphism)
Ex. Function overloading, operator overloading

2. Dynamic Polymorphism(Run time Polymorphism)
Ex. Function overriding

How runtime calls methods in these scenarios? 
First CLR checks runtime type of object, then starts searching last derived form of called method from variable type(compile time type) class to the runtime type class, and calls the last derived form of the called method in between this path. 


namespace Animals
{
    class Humans
    {
        public virtual void walk()
        {
           Console.writeline("Human can walk.");
        }

    class Indians : Humans
    {
        public override void walk()
        {
            Console.writeline("Indians can walk fast.");
        }
    }
}

class mainclass
{
    void main()
    {
        Humans ObjHumans;
     
        ObjHumans = new Indians();
        ObjHumans.Walk(); // will call Indians' walk function

        ObjHumans = new Humans();
        ObjHumans.Walk(); // will call Human's walk function
    }
}

Refer http://gstomar.blogspot.in/2012/02/overriding-and-new-keywords.html

If you want your derived member to have the same name as a member in a base class, but you do not want it to participate in virtual invocation, you can use the new keyword. The newkeyword is put before the return type of a class member that is being replaced. The following code provides an example:
public class BaseClass
{
public void DoWork() { WorkField++; }
public int WorkField;
public int WorkProperty
{
get { return 0; }
}
}
public class DerivedClass : BaseClass
{
public new void DoWork() { WorkField++; }
public new int WorkField;
public new int WorkProperty
{
get { return 0; }
}
}
Hidden base class members can still be accessed from client code by casting the instance of the derived class to an instance of the base class. For example:
DerivedClass B = new DerivedClass();
B.DoWork(); // Calls the new method.
BaseClass A = (BaseClass)B;
A.DoWork(); // Calls the old method.

Function overloading
It’s a technique that enables a function name to accept different nos. and types of parameters. In other word we can create a no. of functions in a class of same name with different no. and different type of parameters.
Function overloading can be performed by
1. Different no. of parameters
2. Different type of parameters
3. Different sequence of parameters
Benefits \ Requirement
1. Makes consistency of interface.
2. No need to remember a no. of functions.
Return type can be changed of different overloaded methods.
Only change of return type can not overload a method, compiler will fire an error “you cannot overload a method by changing only return type
Ex.
Public class Words
{
Private string Getword()
{
// Will return first word of entered string
}
Private string Getword(int index)
{
// Will return word, searching by index from entered string
}
Private string Getword(string word)
{
// Will return word, by searching passed word from entered string
}
}

Function overriding 
A good article on overriding given on Code project
http://www.codeproject.com/KB/cs/cs_methodoverride.aspx

A method of base class can be re-defined in derived class this technique is called function overriding.
In other words if you want a method of base class behave something different for drive class then you can re-define it in derived class.

Method should be marked virtual (overridable in VB) in base class.
When you override it in derived class it will mark override (overrides in VB).

Signature (return type and parameters) of base and overridden methods must be same.
Access modifier of base and overridden methods must be same.
Static or non virtual method can’t be overridden.
An overridden method can’t be marked virtual, abstract or static.
It can be marked sealed so that you can stop it to overriden in further drived calsses.

Class Shapes
{
Public class Square
{
Public double x;
Public square(double x) //constructor
{
this.x = x;
}
Public virtual double area()
{
Return x * x;
}
}

Class Cube : Square
{
Public double x;

public cube(double x) //constructor
{
this.x = x;
}

public override double area()
{
return 6 * x* x;
}

}
Public static void main()
{
Double x = 2.6;
Square S = new Square(x);
Square C = new Cube(x);
Response.write(“Area of Square:” + S.area().tostring());
Response.write(“Area of Cube:” + C.area().tostring());
}
}

CI/CD - Safe DB Changes/Migrations

Safe DB Migrations means updating your database schema without breaking the running application and without downtime . In real systems (A...