Tuesday, March 19, 2013

Abstract Classes and Abstract Methods in C#.Net


Abstract Classes:

-A class which contains any abstract methods in it is known as an abstract class.

-What is an Abstract Method ?

Ans: A method which doesn't contain any method body to it is known as an abstract method.
-An abstract method should be declared with the "abstract" modifier.

eg: public abstract void Add();

-If a method is declared as abstract in a class the implementaion of the method has to be given by its child class which is mandatory for the child using the "override" modifier.

-The concept is similar to your method overriding but in case of overriding it is optional for the child class to override the method but in case of abstract methods it is mandatory for the child class to override or implement the method.

Note: If these abstract methods were defined for u under a class that class is known as an abstract which should be declared using the "abstract" modifier.

eg:
abstract class AbsDemo
{
   public abstract void Add();
}

-What does an abstract class can contain in it ?

Ans: An abstract class can contain both abstract and non-abstract methods in it.
-If the non-abstract methods of an abstract class wants to be consumed by a child class first it needs to give the implementaion for the abstract methods of its parent.

-An abstract class is never useful for itself, it can be consumed only it's child after implementing the abstract methods in it, the reason why it is not useful for it self is the object of an abstract class cannot be created using the "new" operator.

No comments:

Post a Comment

amazon

Sukanya Samriddhi Account - SBI

SUKANYA SAMRIDDHI Account information by SBI SUKANYA SAMRIDDHI ACCOUNT : FACILITY AVAILABLE AT ALL BRANCHES OF SBI Sukanya Samriddhi ...