Abstract Class:
Abstract class is a class that has no direct instances, but whose descendants may have direct instances. There are case in which it is useful to define classes for which the programmer never intends to instantiate any objects; because such classes normally are used as bae-classes in inheritance hierarchies, we call such classes abstract classes These classes cannot be used to instantiate objects; because abstract classes are incomplete. Derived classes called concrete classesmust define the missing pieces.
Abstract classes normally contain one or more abstract methods or abstract properties, such methods or properties do not provide implementations, but our derived classes must override inherited abstract methods or properties to enable obejcts ot those derived classes to be instantiated, not to override those methods or properties in derived classes is syntax error, unless the derived class also is an abstract class.
In some cases, abstract classes constitute the top few levels of the hierarchy, for Example abstract class Shape with abstract method Draw() has tow derived abstract classe Shape2D & Shape3D inherites the method Draw() & also do not provide any implementation for it. Now we have normal classes Rectangle, Square & Circle inherites from Shape2D, and another group of classes Sphere, Cylinder & Cube inherites from Shape3D. All classes at the bottom of the hierarchy must override the abstract method Draw().
A class is made abstract by declaring it with Keyword abstract.
Example:
public abstract class Shape
{
//...Class implementation
public abstract void Draw(int x, int y)
{
//this method mustn't be implemented here.
//If we do implement it, the result is a Syntax Error.
}
}
public abstract class Shape2D : Shape
{
//...Class implementation
//...you do not have to implement the the method Draw(int x, int y)
}
public class Cricle : Shape2D
{
//here we should provide an implemetation for Draw(int x, int y)
public override void Draw(int x, int y)
{
//draw the shape
}
}
Difference between an abstract method & virtual method:
Virtual method has an implementation & provide the derived class with the option of overriding it. Abstract method does not provide an implementation & forces the derived class to override the method.
Important Notes:
(a)Any Class with abstract method or property in it must be declared abstract
(b)Attempting to instantiate an object of an abstract class retults in a compilation error
Example:
Shape m_MyShape = new Shape(); //it is Wrong to that.
But we can do that.
Shape m_MyShape = new Circle(); // True
Or
Shape m_MyShape;
/*
declare refrences only, and the refrences can refer to intances of
any concrete classes derived from abstract class
*/
Circle m_MyCircle = new Circle();
m_MyShape = m_MyCircle; // Also True
(d)An abstract class can have instance data and non-abstract methods -including constructors-.
Jun 27, 2008
Polymorphism, Method Hiding and Overriding in C#
Polymorphism
One of the fundamental concepts of object oriented software development is polymorphism. The term polymorphism (from the Greek meaning "having multiple forms") in OO is the characteristic of being able to assign a different meaning or usage to something in different contexts - specifically, to allow a variable to refer to more than one type of object.
C# Example
Let's assume the following simple classes A and B for the discussions in this text. A is the base class, B is derived from A.
Inherited Methods
A method Foo() which is declared in the base class A and not redeclared in classes B or C is inherited in the two subclasses
using System;
namespace Polymorphism
{
class A
{
public void Foo() { Console.WriteLine("A::Foo()"); }
}
class B : A {}
class Test
{
static void Main(string[] args)
{
A a = new A();
a.Foo(); // output --> "A::Foo()"
B b = new B();
b.Foo(); // output --> "A::Foo()"
}
}
}
The method Foo() can be overridden in classes B and C:
using System;
namespace Polymorphism
{
class A
{
public void Foo() { Console.WriteLine("A::Foo()"); }
}
class B : A
{
public void Foo() { Console.WriteLine("B::Foo()"); }
}
class Test
{
static void Main(string[] args)
{
A a;
B b;
a = new A();
b = new B();
a.Foo(); // output --> "A::Foo()"
b.Foo(); // output --> "B::Foo()"
a = new B();
a.Foo(); // output --> "A::Foo()"
}
}
}
There are two problems with this code.
The output is not really what we, expected. The method Foo() is a non-virtual method. C# requires the use of the keyword virtual in order for a method to actually be virtual.
Although the code compiles and runs, the compiler produces a warning:
...\polymorphism.cs(11,15): warning CS0108: The keyword new is required on 'Polymorphism.B.Foo()' because it hides inherited member 'Polymorphism.A.Foo()'
Virtual and Overridden Methods
Only if a method is declared virtual, derived classes can override this method if they are explicitly declared to override the virtual base class method with the override keyword.
using System;
namespace Polymorphism
{
class A
{
public virtual void Foo() { Console.WriteLine("A::Foo()"); }
}
class B : A
{
public override void Foo() { Console.WriteLine("B::Foo()"); }
}
class Test
{
static void Main(string[] args)
{
A a;
B b;
a = new A();
b = new B();
a.Foo(); // output --> "A::Foo()"
b.Foo(); // output --> "B::Foo()"
a = new B();
a.Foo(); // output --> "B::Foo()"
}
}
}
Method Hiding
Why did the compiler in the second listing generate a warning? Because C# not only supports method overriding, but also method hiding. Simply put, if a method is not overriding the derived method, it is hiding it. A hiding method has to be declared using the new keyword. The correct class definition in the second listing is thus:
using System;
namespace Polymorphism
{
class A
{
public void Foo() { Console.WriteLine("A::Foo()"); }
}
class B : A
{
public new void Foo() { Console.WriteLine("B::Foo()"); }
}
class Test
{
static void Main(string[] args)
{
A a;
B b;
a = new A();
b = new B();
a.Foo(); // output --> "A::Foo()"
b.Foo(); // output --> "B::Foo()"
a = new B();
a.Foo(); // output --> "A::Foo()"
}
}
}
Combining Method Overriding and Hiding
Methods of a derived class can both be virtual and at the same time hide the derived method. In order to declare such a method, both keywords virtual and new have to be used in the method declaration:
class A
{
public void Foo() {}
}
class B : A
{
public virtual new void Foo() {}
}
A class C can now declare a method Foo() that either overrides or hides Foo() from class B:
class C : B
{
public override void Foo() {}
// or
public new void Foo() {}
}
Important things to remember -->
1. If you are using same name in derived class it hides the base class method, and generate a compiler warning. Use new keyword to supress this warning and tells the compiler that ok i am hiding it intentional.
2. Else define the base as virtual and write override in derived class method.
3. If you dont sure whether you want to hide or override use both virtual and new in base class method definition.
One of the fundamental concepts of object oriented software development is polymorphism. The term polymorphism (from the Greek meaning "having multiple forms") in OO is the characteristic of being able to assign a different meaning or usage to something in different contexts - specifically, to allow a variable to refer to more than one type of object.
C# Example
Let's assume the following simple classes A and B for the discussions in this text. A is the base class, B is derived from A.
Inherited Methods
A method Foo() which is declared in the base class A and not redeclared in classes B or C is inherited in the two subclasses
using System;
namespace Polymorphism
{
class A
{
public void Foo() { Console.WriteLine("A::Foo()"); }
}
class B : A {}
class Test
{
static void Main(string[] args)
{
A a = new A();
a.Foo(); // output --> "A::Foo()"
B b = new B();
b.Foo(); // output --> "A::Foo()"
}
}
}
The method Foo() can be overridden in classes B and C:
using System;
namespace Polymorphism
{
class A
{
public void Foo() { Console.WriteLine("A::Foo()"); }
}
class B : A
{
public void Foo() { Console.WriteLine("B::Foo()"); }
}
class Test
{
static void Main(string[] args)
{
A a;
B b;
a = new A();
b = new B();
a.Foo(); // output --> "A::Foo()"
b.Foo(); // output --> "B::Foo()"
a = new B();
a.Foo(); // output --> "A::Foo()"
}
}
}
There are two problems with this code.
The output is not really what we, expected. The method Foo() is a non-virtual method. C# requires the use of the keyword virtual in order for a method to actually be virtual.
Although the code compiles and runs, the compiler produces a warning:
...\polymorphism.cs(11,15): warning CS0108: The keyword new is required on 'Polymorphism.B.Foo()' because it hides inherited member 'Polymorphism.A.Foo()'
Virtual and Overridden Methods
Only if a method is declared virtual, derived classes can override this method if they are explicitly declared to override the virtual base class method with the override keyword.
using System;
namespace Polymorphism
{
class A
{
public virtual void Foo() { Console.WriteLine("A::Foo()"); }
}
class B : A
{
public override void Foo() { Console.WriteLine("B::Foo()"); }
}
class Test
{
static void Main(string[] args)
{
A a;
B b;
a = new A();
b = new B();
a.Foo(); // output --> "A::Foo()"
b.Foo(); // output --> "B::Foo()"
a = new B();
a.Foo(); // output --> "B::Foo()"
}
}
}
Method Hiding
Why did the compiler in the second listing generate a warning? Because C# not only supports method overriding, but also method hiding. Simply put, if a method is not overriding the derived method, it is hiding it. A hiding method has to be declared using the new keyword. The correct class definition in the second listing is thus:
using System;
namespace Polymorphism
{
class A
{
public void Foo() { Console.WriteLine("A::Foo()"); }
}
class B : A
{
public new void Foo() { Console.WriteLine("B::Foo()"); }
}
class Test
{
static void Main(string[] args)
{
A a;
B b;
a = new A();
b = new B();
a.Foo(); // output --> "A::Foo()"
b.Foo(); // output --> "B::Foo()"
a = new B();
a.Foo(); // output --> "A::Foo()"
}
}
}
Combining Method Overriding and Hiding
Methods of a derived class can both be virtual and at the same time hide the derived method. In order to declare such a method, both keywords virtual and new have to be used in the method declaration:
class A
{
public void Foo() {}
}
class B : A
{
public virtual new void Foo() {}
}
A class C can now declare a method Foo() that either overrides or hides Foo() from class B:
class C : B
{
public override void Foo() {}
// or
public new void Foo() {}
}
Important things to remember -->
1. If you are using same name in derived class it hides the base class method, and generate a compiler warning. Use new keyword to supress this warning and tells the compiler that ok i am hiding it intentional.
2. Else define the base as virtual and write override in derived class method.
3. If you dont sure whether you want to hide or override use both virtual and new in base class method definition.
Jun 26, 2008
Creating a DataTable | DataTable in C#
Creating a DataTable | DataTable in C#
DataTable aTable = new DataTable();
aTable.Columns.Add("ProductID", typeof(int));
aTable.Columns.Add("ProductName", typeof(string));
DataRow dr;
dr = aTable.NewRow();
dr[0] = 12;
dr[1] = "Lap Top";
aTable.Rows.Add(dr);
dr = aTable.NewRow();
dr[0] = 13;
dr[1] = "DeskTop";
aTable.Rows.Add(dr);
dr = aTable.NewRow();
dr[0] = 14;
dr[1] = "Server";
aTable.Rows.Add(dr);
dr = aTable.NewRow();
dr[0] = 15;
dr[1] = "Mac PC";
aTable.Rows.Add(dr);
You can create these rows by getting data from database also
command=new SqlCommand("select * from Product",connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while(reader.Read())
{
Dr=aTable.NewRow();
Dr[0] = reader.GetValue(1);
Dr[1] = reader.GetValue(2);
aTable.Rows.Add(Dr);
}
DataTable aTable = new DataTable();
aTable.Columns.Add("ProductID", typeof(int));
aTable.Columns.Add("ProductName", typeof(string));
DataRow dr;
dr = aTable.NewRow();
dr[0] = 12;
dr[1] = "Lap Top";
aTable.Rows.Add(dr);
dr = aTable.NewRow();
dr[0] = 13;
dr[1] = "DeskTop";
aTable.Rows.Add(dr);
dr = aTable.NewRow();
dr[0] = 14;
dr[1] = "Server";
aTable.Rows.Add(dr);
dr = aTable.NewRow();
dr[0] = 15;
dr[1] = "Mac PC";
aTable.Rows.Add(dr);
You can create these rows by getting data from database also
command=new SqlCommand("select * from Product",connection);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
while(reader.Read())
{
Dr=aTable.NewRow();
Dr[0] = reader.GetValue(1);
Dr[1] = reader.GetValue(2);
aTable.Rows.Add(Dr);
}
Jun 22, 2008
Haj committee India | Haj 2008 Lucknow
All about Haj, please visit http://www.hajcommittee.com
This year UP haj pilgrims will be decided on lottery draw due to more responces of limited quota assigned to UP. The lucky draw was decided to held on 21st June 08, and the final list of pilgrims will be posted on haj committee site.
Lets wait and see Whom Allah give a chance to perform Haj this year.
Finally wait is over you can get the results here
This year UP haj pilgrims will be decided on lottery draw due to more responces of limited quota assigned to UP. The lucky draw was decided to held on 21st June 08, and the final list of pilgrims will be posted on haj committee site.
Lets wait and see Whom Allah give a chance to perform Haj this year.
Finally wait is over you can get the results here
Jun 12, 2008
Top 20 2008 Antivirus Software
To Download click the link below:
BitDefender
Kaspersky
ESET Nod32
AVG Anti-Virus
F-Secure
Anti-Virus
Trend
Micro Antivirus
McAfee
VirusScan
Norton
AntiVirus
CA
Antivirus
Norman Antivirus and Antispyware
G DATA Antivirus
Panda Antivirus
AVAST!
F-Prot
PC
Tools AntiVirus
Webroot
Antivirus
ViRobot
CyberScrub
AntiVirus
The
Shield Antivirus
WinAntiVirus
Windows Live OneCare
BitDefender
Kaspersky
ESET Nod32
AVG Anti-Virus
F-Secure
Anti-Virus
Trend
Micro Antivirus
McAfee
VirusScan
Norton
AntiVirus
CA
Antivirus
Norman Antivirus and Antispyware
G DATA Antivirus
Panda Antivirus
AVAST!
F-Prot
PC
Tools AntiVirus
Webroot
Antivirus
ViRobot
CyberScrub
AntiVirus
The
Shield Antivirus
WinAntiVirus
Windows Live OneCare
Jun 10, 2008
AMU 2008 Test courses results
AMU 2008 Test Results
B. Arch.
B.A.L.L.B.
B.C.A./B.I.T./B.C.M.
B.Ed.
B.E. Evening
B.Lib.
B. Sc. (Hons.) Indus. Chemistry
B.Tech.
C.E.T.
D.C.P.
Dip. in Gen. Nursing
L.L.M .
M.B.A./M.B.A.(IB)
M.B.B.S. / B.D.S.
M.C.A.
MD/MS/PG Dip.
M.Ed.
M.Lib.
M.A Mass Comm.
M.S.W. (Final)
XI/Dip. Engg.
All the above courses results can be found at this link.
B. Arch.
B.A.L.L.B.
B.C.A./B.I.T./B.C.M.
B.Ed.
B.E. Evening
B.Lib.
B. Sc. (Hons.) Indus. Chemistry
B.Tech.
C.E.T.
D.C.P.
Dip. in Gen. Nursing
L.L.M .
M.B.A./M.B.A.(IB)
M.B.B.S. / B.D.S.
M.C.A.
MD/MS/PG Dip.
M.Ed.
M.Lib.
M.A Mass Comm.
M.S.W. (Final)
XI/Dip. Engg.
All the above courses results can be found at this link.
Subscribe to:
Posts (Atom)
-
Clustered Index | Non Clustered Index Indexing is used to get fast retrival of data. The indexing which is done on physical storage is known...
-
All about Haj, please visit http://www.hajcommittee.com This year UP haj pilgrims will be decided on lottery draw due to more responces of l...
-
To get all the triggers list and their scripts run the script below. View the result in file view mode. SELECT [text] FROM sysobjects o ...