Abstract Class
These are those classes which cannot be instantiated i.e. their objects cannot be made. We can declare a class abstract by using the keyword abstract before the class keyword.
abstract class ABC
{
.......
}
The important point here to note is that all OOPS concepts apply to the abstract class i.e. whatever we can do within a class can be done here too, like declaring a variable, creating a function, function overloading, function overriding, access modifiers (public, private, protected), inheritance, constructor, destructor, properties, events , indexers, etc. All these concepts can be implemented in a abstract class.
A abstract class can inherit from some other class also:
class A
{.....}
abstract class B : A
{.....}
This means that within a inheritance hierarchy the position of abstract class can be anywhere except the last class i.e. the last child class.
If inheritance is in this order A - >B - > C - > D - > E then all classes except class E can be abstract class.
Further a abstract class can implement interfaces also:
interface I
{......}
abstract class B : I
{.....}
An abstract class can implement combination of both class and interface also, but only one class and any number of interfaces
abstract class B : MyClass, I1, I2, I3, I4
{.....}
Another important point is that if we have declared a constructor in abstract class, it will be called whenever the child class object is made.
class A
{
public A()
{
Console.WriteLine("hello");
}
...}
abstract class B : A
{.....}
B ob1 = new B();
output will be: hello
Interfaces
Interfaces only provide with the declaration of methods, properties, events, indexers, or any combination of those four member types only. An interface cannot contain constants, fields, operators, instance constructors, destructors, or types. It cannot contain static members. Interfaces members are automatically public, and they cannot include any access modifiers. Also Interfaces contain no implementation of methods.
Interface MyInter
{
void Check();
}
A interface can inherit only from any number of interfaces but not from classes. Further an interface cannot be instantiated directly.
It really helped me out in searching the various sp and views that are using a particular table especially considering that the DB is having more than 4200 sp's ! Thanks....
ReplyDeleteHi Jay
ReplyDeleteYou have explained it really well. I am an IT student and it really helped me to understand about abstract class and interface. Good on you.