Archive for the ‘OOPS’ Category

Diffrence between Abstract class and Interface

Feature Interface Abstract class
Multiple inheritance A class may inherit several interfaces. A class may inherit only one abstract class.
Default implementation An interface cannot provide any code, just the signature. An abstract class can provide complete, default code and/or just the details that have to be overridden.
Access Modfiers An interface cannot have access modifiers for the subs, functions, properties etc everything is assumed as public An abstract class can contain access modifiers for the subs, functions, properties
Core VS Peripheral Interfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface. An abstract class defines the core identity of a class and there it is used for objects of the same type.
Homogeneity If various implementations only share method signatures then it is better to use Interfaces. If various implementations are of the same kind and use common behaviour or status then abstract class is better to use.
Speed Requires more time to find the actual method in the corresponding classes. Fast
Adding functionality (Versioning) If we add a new method to an Interface then we have to track down all the implementations of the interface and define implementation for the new method. If we add a new method to an abstract class then we have the option of providing default implementation and therefore all the existing code might work properly.
Fields and Constants No fields can be defined in interfaces An abstract class can have fields and constrants defined
Advertisements

Constructors In C#

Constructors in C# :

Broadly speaking, it is a method in the class which gets executed when its object is created. Usually we put the initialization code in the constructor. Writing a constructor in the class is pretty simple, have a look at the following sample :
 
public class mySampleClass
{
public mySampleClass()
{
// This is the constructor method.
}
// rest of the class members goes here.
}
When the object of this class is instantiated this constructor will be executed. Something like this :Constructor Overloading :

mySampleClass obj = new mySampleClass()
// At this time the code in the constructor will // be executed

C# supports overloading of constructors, that means we can have constructors with different set of parameters. So our class can be like this :

publicclass mySampleClass
{
public mySampleClass()
{
// This is the no parameter constructor method.
// First Constructor
}
public mySampleClass(int Age)
{
// This is the constructor with one parameter.
// Second Constructor
}
public mySampleClass(int Age, string Name)
{
// This is the constructor with two parameters.
// Third Constructor
}
// rest of the class members goes here.
}

Well, note here that call to the constructor now depends on the way you instantiate the object. For example :
new mySampleClass()
// At this time the code of no parameter
// constructor (First Constructor) will be executed
mySampleClass obj = new mySampleClass(12)
// At this time the code of one parameter
// constructor(Second Constructor) will be
// executed.
The call to the constructors is completely governed by the rules of the overloading here.

mySampleClass obj =

Calling Constructor from another Constructor:

You can always make the call to one constructor from within the other. Say for example :

public
class mySampleClass
{
public mySampleClass(): this(10)
{
// This is the no parameter constructor method.
// First Constructor
}
public mySampleClass(int Age)
{
// This is the constructor with one parameter.
// Second Constructor
}


Very first of all let us see what is this syntax :     mySampleClass(): this(10)

Here this refers to same class, so when we say this(10), we actually mean execute the public mySampleClass(int Age) method.The above way of calling the method is called initializer. We can have at the most one initializer in this way in the method.

Another thing which we must know is the execution sequence i.e., which method will be executed when. Here if I instantiate the object as new mySampleClass()

Then the code of public mySampleClass(int Age) will be executed before the code of mySampleClass(). So practically the definition of the method : 

public
mySampleClass(): this(10)
{
// This is the no parameter constructor method.
// First Constructor
}is equivalent to :public mySampleClass()
{
mySampleClass(10)
// This is the no parameter constructor method.
// First Constructor
}
Note that only this and base (we will see it further) keywords are allowed in initializers, other method calls will raise the error.

mySampleClass obj =

This is sometimes called Constructor chaining.

Behavior of Constructors in Inheritance :

Let us first create the inherited class.

publicclass myBaseClass
{
public myBaseClass()
{
// Code for First Base class Constructor
}
public myBaseClass(int Age)
{
// Code for Second Base class Constructor
}
// Other class members goes here
}
public class myDerivedClass : myBaseClass
// Note that I am inheriting the class here.
{
public myDerivedClass()
{
// Code for the First myDerivedClass Constructor.
}
public myDerivedClass(int Age):base(Age)
{
// Code for the Second myDerivedClass Constructor.
}
// Other class members goes here
}
Now what will be the execution sequence here :Then the sequence of execution will be 2. and then public myDerivedClass() method. 

myDerivedClass obj =

Note: If we do not provide initializer referring to the base class constructor then it executes the no parameter constructor of the base class.

Note one thing here : We are not making any explicit call to the constructor of base class neither by initializer nor by the base() keyword, but it is still executing. This is the normal behavior of the constructor.

If I create the object of the Derived class as 

myDerivedClass obj =
new myDerivedClass(15)

Then the sequence of execution will be 

1. public myBaseClass(int Age) method.
2. and then public myDerivedClass(int Age) method.

Here the new keyword base has come into picture. This refers to the base class of the current class. So, here it refers to the myBaseClass. And base(10) refers to the call to myBaseClass(int Age) method.

Also note the usage of Age variable in the syntax :public myDerivedClass(int Age):base(Age).Understanding it is left to the reader.

Static Constructors :

This is a new concept introduced in C#. By new here I mean that it was not available for the C++ developers. This is a special constructor and gets called before the first object is created of the class. The time of execution cannot be determined, but it is definitely before the first object creation – could be at the time of loading the assembly.

The syntax of writing the static constructors is also damn simple. Here it is :public class myClass
{
static myClass()
{
// Initialization code goes here.
// Can only access static members here.
}
// Other class methods goes here
}
 

Notes for Static Constructors :

1. There can be only one static constructor in the class.
2. The static constructor should be without parameters.
3. It can only access the static members of the class.
4. There should be no access modifier in static constructor definition.

Ok fine, all the above points are fine but why is it like that? Let us go step by step here.

Firstly, the call to the static method is made by the CLR and not by the object, so we do not need to have the access modifier to it.

Secondly, it is going to be called by CLR, who can pass the parameters to it, if required, No one, so we cannot have parameterized static constructor.

Thirdly, Non-static members in the class are specific to the object instance so static constructor, if allowed to work on non-static members, will reflect the changes in all the object instances, which is impractical. So static constructor can access only static members of the class.

Fourthly, Overloading needs the two methods to be different in terms to methods definition, which you cannot do with Static Constructors, so you can have at the most one static constructor in the class.

Now, one question raises here, can we have the two constructors as public class myClass
{
static myClass()
{
// Initialization code goes here.
// Can only access static members here.
}
public myClass()
{
// Code for the First myDerivedClass Constructor.
}
// Other class methods goes here
}

This is perfectly valid, though doesn’t seem to be in accordance with overloading concepts. But why? Because the time of execution of the two method are different. One is at the time of loading the assembly and one is at the time of object creation.

FAQs Regd. Constructors :

1. Is the Constructor mandatory for the class ?

Yes, It is mandatory to have the constructor in the class and that too should be accessible for the object i.e., it should have a proper access modifier. Say for example we have the private constructor in the class then it is of no use as it cannot be accessed by the object, so practically it is no available for the object. In such   conditions it will raise an error.

2. What if I do not write the constructor ?

In such case the compiler will try to supply the no parameter constructor for your class behind the scene. Compiler will attempt this only if you do not write the constructor for the class. If you provide any constructor ( with or without parameters), then compiler will not make any such attempt.

3. What if I have the constructor public myDerivedClass() but not the public myBaseClass() ? 

It will raise an error. If either the no parameter constructor is absent or it is in-accessible ( say it is private ), it will raise an error. You will have to take the precaution here.

4. Can we access static members from the non-static ( normal ) constructors ?

Yes, We can. There is no such restriction on non-static constructors. But there is one on static constructors that it can access only static members.

If I create the object of the Derived class as

new myDerivedClass()

1. public myBaseClass() method

public

Facts about static classes and constructors

Static Classes

 Static classes and class members are used to create data and functions that can be accessed without creating an instance of the class. Static class members can be used to separate data and behavior that is independent of any object identity: the data and functions do not change regardless of what happens to the object. Static classes can be used when there is no data or behavior in the class that depends on object identity.

A class can be declared static, indicating that it contains only static members. It is not possible to create instances of a static class using the new keyword. Static classes are loaded automatically by the .NET Framework common language runtime (CLR) when the program or namespace containing the class is loaded. Use a static class to contain methods that are not associated with a particular object.

For example, it is a common requirement to create a set of methods that do not act on instance data and are not associated to a specific object in your code. You could use a static class to hold those methods.

The main features of a static class are:

  • They only contain static members.
  • They cannot be instantiated.
  • They are sealed.
  •  They cannot contain Instance Constructors.

Creating a static class is therefore much the same as creating a class that contains only static members and a private constructor. A private constructor prevents the class from being instantiated. The advantage of using a static class is that the compiler can check to make sure that no instance members are accidentally added. The compiler will guarantee that instances of this class cannot be created. Static classes are sealed and therefore cannot be inherited. Static classes cannot contain a constructor, although it is still possible to declare a static constructor to assign initial values or set up some static state. 

 When to Use Static Classes

When methods do not need to be attached to a specific instance of the class.

 Use a static class as a unit of organization for methods not associated with particular objects. Also, a static class can make your implementation simpler and faster because you do not have to create an object in order to call its methods. It is useful to organize the methods inside the class in a meaningful way, such as the methods of the Math class in the System namespace.

Static Members

A static method, field, property, or event is callable on a class even when no instance of the class has been created. If any instances of the class are created, they cannot be used to access the static member. Only one copy of static fields and events exists, and static methods and properties can only access static fields and static events. Static members are often used to represent data or calculations that do not change in response to object state; for instance, a math library might contain static methods for calculating sine and cosine.

  • Static class members are declared using the static keyword before the return type of the member.
  • Static members are initialized before the static member is accessed for the first time, and before the static constructor, if any is called.
  • To access a static class member, use the name of the class instead of a variable name to specify the location of the member.

 

 Static constructors 

Static constructors have the following properties:

  •  A static constructor does not take access modifiers or have parameters. A static constructor is used to initialize any static data, or to perform a particular action that needs performed once only.
  • It is called automatically before the first instance is created or any static members are referenced.
  • A static constructor is called automatically to initialize the class before the first instance is created or any static members are referenced.
  •  A static constructor cannot be called directly.
  • The user has no control on when the static constructor is executed in the program.
  •  A typical use of static constructors is when the class is using a log file and the constructor is used to write entries to this file.
  •  Static constructors are also useful when creating wrapper classes for unmanaged code, when the constructor can call the LoadLibrary method.

 

Reference Online MSDN

Refer this link for more detail http://msdn.microsoft.com/en-us/library/79b3xss3(VS.80).aspx

When to use a static class

A few days back I was having a conversation with my colleague on the use of static class. At that point I decided to write a post on the issue giving an example on when to use a static class and members.

We use static class when we have to separate data and behavior that will be independent of any object identity. The data and functions do not change regardless of what happens to the object.

A static class can only contain static members. We cannot create an instance of a static class. Static class is always sealed and they cannot contain instance constructor. Hence we can also say that creating a static class is nearly same as creating a class with only static members and a private constructor (private constructor prevents the class from being instantiated).

The advantage of using the static class is that compiler can check that no instance of the class is accidentally added. The complier will not allow any instance class of a static class. We also cannot inherit a static class since it is sealed. Static class do not have constructor, but can still declare static constructor to set up the initial values.

Static class also makes the implementation simpler and faster since we do not have make and instance of the class to call its method.  An example of good use of static class would be a class like math, which does all the mathematic function, or a currency converter class to convert currency class for converting the currency.

Hope this helps

http://msjawahar.wordpress.com/2007/07/24/when-to-use-a-static-class/

accordion

a awesome accordion menu link very simply and easy to use

http://www.stickmanlabs.com/accordion/

Implementations

  1. Include the files

You need to include accordion.js but it requires prototype.js and effects.js (like I said this is for scriptaculous!). As long as you are using at least the latest stable builds you are fine.

<script type="text/javascript" src="javascript/prototype.js"></script>
<script type="text/javascript" src="javascript/effects.js"></script>
<script type="text/javascript" src="javascript/accordion.js"></script>

2.)The Markup

Pretty much anything will work, so its really up to you. The presentation is up to you (see the source of this page for some examples). Ideally you should probably use header tags with div’s containing the content as this would be best symantically speaking and for seo. Below is a basic example.

<h2 class="accordion_toggle">Title Bar</h2>
<div class="accordion_content">...</div>
...
...
...
<h2 class="accordion_toggle">Title Bar</h2>
<div class="accordion_content">...</div>



  Normal
  0

  false
  false
  false

  EN-US
  X-NONE
  X-NONE

  MicrosoftInternetExplorer4





<!--[endif]-->

For nested accordion

<div id =” vertical_nested_container”>

<h2 class="accordion_toggle">Title Bar</h2>

<div class="accordion_content">...</div>

...

...

<div class=” accordion_content”>

<div id=” vertical_nested_container”>

<h2 class="vertical_accordion_toggle">Title Bar</h2>

<div class="vertical_accordion_content">...</div>

...

...

</div>

</div>

...

<h2 class="accordion_toggle">Title Bar</h2>

<div class="accordion_content">...</div>

4. Vertical and horizontal accorion
// General Syntax
new accordion('container-selector', options);

// Horizontal example
var horizontalAccordion = new accordion('#top_container', {
    classNames : {
        toggle : 'horizontal_accordion_toggle',
        toggleActive : 'horizontal_accordion_toggle_active',
        content : 'horizontal_accordion_content'
    },
    defaultSize : {
        width : 525
    },
    direction : 'horizontal'
});
 to open the menu default
// Let's create it
var verticalAccordion = new accordion('#bottom_container');

// Now lets open the first slide
verticalAccordion.activate($$('#bottom_container .accordion_toggle')[0]);


// Vertical Accordion
var verticalAccordion = new accordion('#bottom_container');

to open the sub menu default

'...........

<!–[if gte mso 9]> Normal 0 false false false EN-US X-NONE X-NONE MicrosoftInternetExplorer4 <![endif]–><!–[if gte mso 9]> <![endif]–> <!–[endif]–>

For nested accordion

<h2 class=“accordion_toggle”>Title Bar</h2>

<div class=“accordion_content”>…</div>

<h2 class=“accordion_toggle”>Title Bar</h2>

<div class=“accordion_content”>…</div>

 

 

 

 

 

<h2 class=“accordion_toggle”>Title Bar</h2>

<div class=“accordion_content”>…</div>

3.Vertical, one horizontal

Below is how I created the two accordions you see on this page, one vertical, one horizontal with a specified width.

// General Syntax
new accordion('container-selector', options);
 
// Horizontal example
var horizontalAccordion = new accordion('#top_container', {
    classNames : {
        toggle : 'horizontal_accordion_toggle',
        toggleActive : 'horizontal_accordion_toggle_active',
        content : 'horizontal_accordion_content'
    },
    defaultSize : {
        width : 525
    },
    direction : 'horizontal'
});
 
// Vertical Accordion
var verticalAccordion = new accordion('#bottom_container');