Tuesday, January 31, 2012

session not using in windows forms

Windows Form app are stateful application, therefore maintaince of state is not required. If you want to share information across forms then you can have a static class/ variables/ for the same.


What is Abstract Class?


Abstract class is a class that can not be instantiated, it exists extensively for inheritance and it must be inherited. There are scenarios in which it is useful to define classes that is not intended to instantiate; because such classes normally are used as base-classes in inheritance hierarchies, we call such classes abstract classes. 

Abstract classes cannot be used to instantiate objects; because abstract classes are incomplete, it may contain only definition of the properties or methods and derived classes that inherit this implements it's properties or methods. 

Static, Value Types & interface doesn't support abstract modifiers. Static members cannot be abstract. Classes with abstract member must also be abstract. 

Abstract classes are classes that contain one or more abstract methods (methods without implementation). An abstract method is a method that is declared, but doesn't contain implementation (like method declaration in the interface). Abstract classes can't be instantiated, and require subclasses to provide implementations for the abstract methods. This class must be inhertied. This class is mostly used as a base class.



Questions on Abstract class are very frequently asked in interviews:) Apart from interviews Abstract class is also very important to know when you are designing or working on a real time applications that needs proper design. I am not expert in this however trying to explain what I know out of my limited knowledge. This article tries to cover Abstract class, Abstract method, Abstract property and difference between abstract method and virtual method.

Abstract class is a class that can not be instantiated. To use it you need to inherit it. This class can be used as a base class where you can define certain method that must be implemented in derived class (Class that is going to inherit it) along with that you can also define certain methods that is frequently used and can be directly used by Derived class or overriden in the derived class if needed.
In the abstract class, you can define following:
  1. Normal property - this property is similar to any other property we define in a normal class
  2. Abstract property - this will have only get and set accessor but no implementation.
  3. Normal method - this method is similar to any other method that you define in a normal class
  4. Abstract method - this will not have any implementation
  5. Virtual method - this will have implementation but can also be overridden in the derived class to provide additional logic or completely replace its logic

Abstract Classes: Classes which cannot be instantiated. This means one cannot make a object of this class or in other way cannot create object by saying ClassAbs abs = new ClassAbs(); where ClassAbs is abstract class. 
Abstract classes contains have one or more abstarct methods, ie method body only no implementation. 
Interfaces: These are same as abstract classes only difference is we can only define method definition and no implementation. 
When to use wot depends on various reasons. One being design choice. 
One reason for using abstarct classes is we can code common 
functionality and force our developer to use it. I can have a complete 
class but I can still mark the class as abstract. 
Developing by interface helps in object based communication.
Abstract Class
In order to explain Abstract class, I am going to take a simple example of ParentClass class that has all methods and properties explained above and it looks like below
ParentClass.cs
using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

/// <summary>

/// Summary description for ParentClass

/// </summary>

public abstract class ParentClass

{
public ParentClass()
{

}
private int age = 0;
// Normal property
public int Age_Normal
{
get
{return age;
}
set
{age = value;
}
}
// Abstract property
public abstract string Address_Abstract
{
get;
set;
}

// Normal Methods
public string GetName(string firstName, string lastName)
{
return "My Name is : " + GetName_Virtual(firstName, lastName);
}
public int Divide_NotAbstract(int a, int b)
{
return a / b;
}
// Abstract Methods
public abstract int Add_Abstract(int a, int b);
public abstract int Subtract_Abstract(int a, int b);

// Virtual method
public virtual string GetName_Virtual(string firstName, string lastName)
{
return firstName + " " + lastName;
}
}
Get solutions of .NET problems with video explanations, .pdf and source code in .NET How to's.
As you can see, the first property I have above is Age_Normal, this is a normal property similar to other property that we define in the class that has its implementation as well and it can be simply accessed by the instance of the dervied class.
Next we have an Abstract property Address_Abstract that has only get and set accessor and no implementation.
Next we have normal method that is similar to any other method we define in a normal class.
Next we have Abstract methods that contains abstract keyword in its definition, this doesn't have any implementation. (Defining abstract methods and properties are similar to defining properties and methods in an interface). One thing to note is that an abstract methods or properties can only be defined in Abstract class, you can't define them in a normal class.
Next we have a virtual method that let us use its logic directly or also allow us to completely override its logic.
Derived Class
Below is my derived class (that is inheriting above abstract class), its name is DerivedClass (Some of the method or property names may look strange, I have just kept this for easy understanding, please bear with me).
DerivedClass.cs
using System;

using System.Data;

using System.Configuration;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

/// <summary>

/// Summary description for Calculate

/// </summary>

public class DerivedClass : ParentClass

{
private string address = string.Empty;
public DerivedClass()
{
}
// override the abstract property
public override string Address_Abstract
{
get
{return address;
}
set
{address = value;
}
}
// override the abstract method
public override int Add_Abstract(int a, int b)
{
return a + b;
}
public override int Subtract_Abstract(int a, int b)
{
return a - b;
}
// override virtual method
public override string GetName_Virtual(string firstName, string lastName)
{
return "(Overriden) Name: " + firstName + " " + lastName;
}
// hide normal method of the abstract class
public new string Divide_NotAbstract(int a, int b)
{
int d = a/b;
return "The division is: " + d.ToString();
}
// use abstract property to retrieve its value
public string GetAddress()
{
return Address_Abstract;
}
}
In the above class I am doing following:
First, I am overriding the Address_Abstract property, as I had declared it as Abstract property. Note that to implement the abstract property or method, you need to use override keyword.
Next, I am overriding two methods Add_Abstract and Sub_Abstract as these methods are declared as Abstract method.
Next I had declared GetName_Virtual method as virtual method so I have freedom to either override it or use it as it is. If we want to use the original method, we can choose not to override it but if we want to modify its logic then we will have to override it. I preferred to override so I have redefined my logic here.
Next I have made Divide_NoAbstract method of Parent class as hidden by specifying new keyword in the definition of derived class. Even if I have made the original parent class method as hidden if at some place we want to use the orginial abstract class method, we can use that, I will show you how to use that later.
How to use Derived and Abstract class methods
Below is the code that shows how to use above abstract class and derived class methods or properties.
DerivedClass c = new DerivedClass();

Response.Write("<b>Abstract method - Sum: </b>" + c.Add_Abstract(50, 30).ToString() + "<br />");

Response.Write("<b>Abstract method - Subtract: </b>" + c.Subtract_Abstract(50, 30).ToString() + "<br />");

Response.Write("<b>Virtual Method - GetName_Virtual: </b>" + c.GetName_Virtual("SHEO", "NARAYAN") + "<br />");

Response.Write("<b>Normal Public Method - GetName: </b>" + c.GetName("SHEO", "NARAYAN") + "<br />");

Response.Write("<b>Normal Public Method being hidden using new keyword - Divide_NotAbstract: </b>" + c.Divide_NotAbstract(50, 30).ToString() + "<br />");

ParentClass p = new DerivedClass();

Response.Write("<b>Normal Public Method from Abstract Class - Divide_NotAbstract: </b>" + p.Divide_NotAbstract(50, 30).ToString() + "<br />");

c.Address_Abstract = "Sheo Narayan, Hitec City, Hyderabad.";

Response.Write("<b>Normal Public method accessing <br />overriden Abstract Property - GetAddress: </b>" + c.GetAddress() + "<br />");
Above code snippet will give following results

Abstract method - Sum: 80
Abstract method - Subtract: 20
Virtual Method - GetName_Virtual: (Overriden) Name: SHEO NARAYAN
Normal Public Method - GetName: My Name is : (Overriden) Name: SHEO NARAYAN
Normal Public Method being hidden using new keyword - Divide_NotAbstract: The division is: 1
Normal Public Method from Abstract Class - Divide_NotAbstract: 1
Normal Public method accessing
overriden Abstract Property - GetAddress: 
Sheo Narayan, Hitec City, Hyderabad.


In the above code snippet, first I instantiated the DerivedClass method and start calling the both Abstract methods Add_Abstract and Subtract_Abstractthat were implemented in the Derived class.
Next I have called the Virtual method of the Abstract class that was overriden in the Derived class. You can see the implementation of GetName_Virtualmethod in the Derived class that is prefixing "(Overriden) Name" with the result.
Next line is calling a normal method GetName that was defined in the Parent abstract class.
Next line is calling the method Divide_NoAbstract that is hiding the main normal method Divide_NoAbstract of Parent class by specifying the new keyword in its definition in the derived class.
Now lets suppose, you already have made the Parent class method (as explained in the above line) hidden but still in a certain scenario, you want to call the Parent class method. To do that, we need to instantiate the Derived Class by specifying it as the ParentClass by writing it as ParentClass p = new DerivedClass();. This is giving me the reference of Parent class and when called Divide_NoAbstract method, this will call the Parent classDivide_NoAbstract method not Derived class method.
The very next line is setting the property of the Derived class that is nothing but the implementation of the ParentClass Address_Abstract property and calling the Derived class GetAddress method that is simply returning this property value.
Conclusion
In this article I tried to show a practical example of how to use Abstract class and what are differnt things that we can define into it. I also tried to show how to work with methods and properties of Abstract class in different scenarios. Hope this article will be useful for the readers. Please subscribefor the subsequent articles alert directly in your email. NOTE: This article was written in 3 breaks, please let me know if somewhere the continuation is broken. Thanks
http://www.dotnetfunda.com/interview/showcatquestion.aspx?category=42

No comments :

Post a Comment