Saturday, July 21, 2012

Abstract_and_Static_Class


Static Class :

May 11, 2011
A static class is basically the same as a non-static class, but there is one difference: a static class cannot be instantiated. In other words, you cannot use the new keyword to create a variable of the class type. Because there is no instance variable, you access the members of a static class by using the class name itself. 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. Static classes are loaded automatically by the .NET Framework common language runtime (CLR) when the program or namespace containing the class is loaded.

The main features of a static class are:

  • They only contain static members.
  • They cannot be instantiated.
  • They are sealed and therefore cannot be inherited.
  • They cannot contain Instance Constructors (C# Programming Guide).
public static class Settings { static int i; public static string GetName() { return "MyName"; } } class Program { static void Main(string[] args) { string str=Settings.GetName(); Console.Write(str); Console.Read(); } }

Static Fields:

Static fields can be declared as follows by using the keyword static. class MyClass { public static int x; public static int y = 20; }
When we declare a static field inside a class, it can be initialized with a value as shown above. All un-initialized static fields automatically get initialized to their default values when the class is loaded first time.

For example

class MyClass { public static int x = 20; public static int y; public static int z = 25; public MyClass(int i) { x = i; y = i; z = i; } } class MyClient { public static void Main() { Console.WriteLine("{0},{1},{2}", MyClass.x, MyClass.y, MyClass.z); MyClass mc = new MyClass(25); Console.WriteLine("{0},{1},{2}", MyClass.x, MyClass.y, MyClass.z); } }

Static Method:

Static methods can be declared using Static keyword befor method name. The static methods can by accessed directly from the class. Static methods are normally faster to invoke on the call stack than instance methods
class MyClass { private static int x = 20; private static int y = 40; public static void Method() { Console.WriteLine("{0},{1}", x, y); } } class MyClient { public static void Main() { MyClass.Method(); } }

Abstract Class :

An abstract class is the one that is not used to create objects. An abstract class is designed to act as a base class (to be inherited by other classes). Abstract class is a design concept in program development and provides a base upon which other classes are built. Abstract classes are similar to interfaces. After declaring an abstract class, it cannot be instantiated on it's own, it must be inherited. Like interfaces, abstract classes can specify members that must be implemented in inheriting classes. Unlike interfaces, a class can inherit only one abstract class. Abstract classes can only specify members that should be implemented by all inheriting classes.
Important :
  • An abstract class cannot be a sealed class or static.
  • Declarations of abstract methods are only allowed in abstract classes.
  • An abstract method cannot be private.
  • The access modifier of the abstract method should be same in both the abstract class and its derived class. If you declare an abstract method as protected, it should be protected in its derived class. Otherwise, the compiler will raise an error.
public abstract class ABstractClass { static int i; public int GetValue(int first,int second) { return first + second; } } class Program : ABstractClass { static void Main(string[] args) { // Program sd = new Program (); // This will give error as you can not create instance of Static class Program cc = new Program(); int value = cc.GetValue(10, 20); Console.Write("Total : " + value.ToString()); Console.Read(); } }

Abstract method:

An abstract method is a method without any method body. Because an abstract method provides no actual implementation, the method-body of an abstract method simply consists of a semicolon. When a class inherits from an abstract class, the derived class must implement all the abstract methods declared in the base class. But by declaring the derived class also abstract, we can avoid the implementation of all or certain abstract methods. This is what is known as partial implementation of an abstract class
public abstract class Shape { public abstract void Paint(Graphics g, Rectangle r); } public class Ellipse : Shape { public override void Paint(Graphics g, Rectangle r) { g.DrawEllipse(r); } } public class Box : Shape { public override void Paint(Graphics g, Rectangle r) { g.DrawRect(r); } } http://thecodekey.com/C_VB_Codes/Abstract_and_Static_Class.aspx

No comments :

Post a Comment