Wednesday, May 8, 2013

Constructor and Destructor

A constructor is a specialized function that is used to initialize fields. A constructor has the same name as the class. Instance constructors are invoked with the new operator and can't be called in the same manner as other member functions. There are some important rules pertaining to constructors as in the following;



Constructor and Destructor

A constructor is a specialized function that is used to initialize fields. A constructor has the same name as the class. Instance constructors are invoked with the new operator and can't be called in the same manner as other member functions. There are some important rules pertaining to constructors as in the following;



Encapsulation

Encapsulation binds together code and the data it manipulates and keeps them both safe from outside interference and misuse. Encapsulation is a protective container that prevents code and data from being accessed by other code defined outside the container.

Inheritance

Inheritance is the process by which one object acquires the properties of another object. A type derives from a base type, taking all the base type members fields and functions. Inheritance is most useful when you need to add functionality to an existing type. For example all .NET classes inherit from the System.Object class, so a class can include new functionality as well as use the existing object's class functions and properties as well.

Polymorphism

Polymorphism is a feature that allows one interface to be used for a general class of action. This concept is often expressed as "one interface, multiple actions". The specific action is determined by the exact nature of circumstances.


Sealed class
A sealed class is a class that does not allow inheritance. Some object model designs need to allow the creation of new instances but not inheritance, if this is the case, the class should be declared as sealed.
To create a sealed class in C#, the class declaration should be done as:
sealed class Shape To create a sealed class in VB.NET, the class declaration should be done as:
NonInheritable Class Shape Abstraction
Abstraction is "the process of identifying common patterns that have systematic variations; an abstraction represents the common pattern and provides a means for specifying which variation to use" (Richard Gabriel).
An abstract class is a parent class that allows inheritance but can never be instantiated. Abstract classes contain one or more abstract methods that do not have implementation. Abstract classes allow specialization of inherited classes.


Abstraction:

Abstraction is a process of hiding the implementation details and displaying the essential features.

Example1: A Laptop consists of many things such as processor, motherboard, RAM, keyboard, LCD screen, wireless antenna, web camera, usb ports, battery, speakers etc. To use it, you don't need to know how internally LCD screens, keyboard, web camera, battery, wireless antenna, speaker’s works.  You just need to know how to operate the laptop by switching it on. Think about if you would have to call to the engineer who knows all internal details of the laptop before operating it. This would have highly expensive as well as not easy to use everywhere by everyone.

Encapsulation

Encapsulation is the mechanism that binds together code and the data if manipulates, and keeps both safe from outside interference and misuse. One way to think about encapsulation is as a protective wrapper that prevents the code and data from being arbitrarily accessed by other code defined outside the wrapper. Access to the code and data inside the wrapper is tightly controlled through a well-defined interface. Conclusion: The wrapping up of data and methods into a single unit (called class) is known as encapsulation.


Inheritance

Inheritance is the process by which object of one class acquires the properties of another class. Inheritance supports the concept of hierarchical classification. For example, the atlas is a part of class bicycle, which is again a part of the class cycle. As illustrated in the principal behind this sort of division is that each derived class shares common characteristics with the class from which it is derived. In OOP, the concept of inheritance provides the idea of reusability. This means that we can add additional features to an existing class without modifying it. This is possible deriving a new class from the existing one. The new class will have the combined features of both the classes. Thus the real appeal and power of the inheritance mechanism is that allows the programmer to reuse a class that is almost, but not exactly, what he wants, and to tailor the class is such a way that is does not introduce any undesirable side effects into the rest of the class. The drive class is knows as 'subclass'.


Polymorphism

Polymorphism is a feature that allows one interface to be used for a general class of actions. The specific action is determined by the exact nature of the solution. Polymorphism means the ability to take more than one form. For example, consider the operation of addition. For two numbers, the operation will generate the sum. If the operands are three numbers, then the operation would produce the product of them. That a single function name can be used to handle different number and different arguments. This is something similar to a particular word having several different meanings depending on the context. Polymorphism plays an important role in allowing objects having different internal structures to share the same external interface. This means that a general class of operations may be accessed in the same manner even though specific actions associated with each operation may differ. Polymorphism is extensively used in implementing inheritance.

Monday, May 6, 2013

lustered
  • Clustered indexes sort and store the data rows in the table or view based on their key values. These are the columns included in the index definition. There can be only one clustered index per table, because the data rows themselves can be sorted in only one order.
  • The only time the data rows in a table are stored in sorted order is when the table contains a clustered index. When a table has a clustered index, the table is called a clustered table. If a table has no clustered index, its data rows are stored in an unordered structure called a heap.
Nonclustered
  • Nonclustered indexes have a structure separate from the data rows. A nonclustered index contains the nonclustered index key values and each key value entry has a pointer to the data row that contains the key value.
  • The pointer from an index row in a nonclustered index to a data row is called a row locator. The structure of the row locator depends on whether the data pages are stored in a heap or a clustered table. For a heap, a row locator is a pointer to the row. For a clustered table, the row locator is the clustered index key.
  • You can add nonkey columns to the leaf level of the nonclustered index to by-pass existing index key limits, 900 bytes and 16 key columns, and execute fully covered, indexed, queries. For more information, see Create Indexes with Included Columns

    Index Types

    There are two main index types; Clustered index and Non-Clustered index.
    A clustered index alters the way that the rows are stored. When you create a clustered index on a column (or a number of columns), SQL server sorts the table’s rows by that column(s). It is like a dictionary, where all words are sorted in alphabetical order in the entire book. Since it alters the physical storage of the table, only one clustered index can be created per table. In the above example the entire rows are sorted by computer_id since a clustered index on computer_id column has been created.
    CREATE CLUSTERED INDEX [IX_CLUSTERED_COMPUTER_ID] 
    ON [dbo].[nics] ([computer_id] ASC)
    A non-clustered index, on the other hand, does not alter the way the rows are stored in the table. It creates a completely different object within the table that contains the column(s) selected for indexing and a pointer back to the table’s rows containing the data. It is like an index in the last pages of a book, where keywords are sorted and contain the page number to the material of the book for faster reference. A non-clustered index on the computer_id in the previous example would look like the table below:
    CREATE NONCLUSTERED INDEX [IX_NONCLUSTERED_COMPUTER_ID] 
    ON [dbo].[nics] ([computer_id] ASC)
     
     
    Here is the common misconception prevailing in the industry.
    Primary Key has to be Clustered Index. 
    In reality the statement should be corrected as follows:
    Primary Key can be Clustered or Non-clustered but it is a common best practice to create a Primary Key as Clustered Index. 


    Scenario 1 : Primary Key will default to Clustered Index

    In this case we will create only Primary Key and when we check the kind of index created on the table we will notice that it has created clustered index automatically over it.
    -- Case 1 Primary Key Defaults to Clustered Index
    USE TempDB
    GO
    -- Create table
    CREATE TABLE TestTable
    (ID INT NOT NULL PRIMARY KEY,
    Col1 INT NOT NULL)
    GO
    -- Check Indexes
    SELECT OBJECT_NAME(OBJECT_ID) TableObject,
    [name] IndexName,
    [Type_Desc]
    FROM sys.indexes
    WHERE OBJECT_NAME(OBJECT_ID) = 'TestTable'
    GO
    -- Clean up
    DROP TABLE TestTable
    GO