Tuesday, 19 July 2016

Abstract Class


Abstract class is a special type of class which cannot be instantiated and acts as a base class for other classes. Abstract class members marked as abstract must be implemented by derived classes.
The purpose of an abstract class is to provide basic or default functionality as well as common functionality that multiple derived classes can share and override.
For example, a class library may define an abstract class that is used as a parameter to many of its functions, and require programmers using that library to provide their own implementation of the class by creating a derived class. In C#, System.IO.FileStream is an implementation of the System.IO.Stream abstract class.
1.  abstract class ShapesClass
2.  {
3.   abstract public int Area();
4.  }
5.  class Square : ShapesClass
6.  {
7.   int side = 0;
8.   
9.   public Square(int n)
10.  {
11.  side = n;
12.  }
13.  // Override Area method
14.  public override int Area()
15.  {
16.  return side * side;
17.  }
18. }
19.  
20. class Rectangle : ShapesClass
21. {
22.  int length = 0, width=0;
23.  
24.  public Rectangle (int length, int width)
25.  {
26.  this.length = length;
27.  this.width = width;
28.  }
29.  // Override Area method
30.  public override int Area()
31.  {
32.  return length * width;
33.  }
34. }
Features of Abstract Class
1.     An abstract class cannot be instantiated.
2.     An abstract class contain abstract members as well as non-abstract members.
3.     An abstract class cannot be a sealed class because the sealed modifier prevents a class from being inherited and the abstract modifier requires a class to be inherited.
4.     A non-abstract class which is derived from an abstract class must include actual implementations of all the abstract members of parent abstract class.
5.     An abstract class can be inherited from a class and one or more interfaces.
6.     An Abstract class can has access modifiers like private, protected, internal with class members. But abstract members cannot have private access modifier.
7.     An Abstract class can has instance variables (like constants and fields).
8.     An abstract class can has constructors and destructor.
9.     An abstract method is implicitly a virtual method.
10.  Abstract properties behave like abstract methods.
11.  An abstract class cannot be inherited by structures.
12.  An abstract class cannot support multiple inheritance.
Common design guidelines for Abstract Class
1.     Don't define public constructors within abstract class. Since abstract class cannot be instantiate and constructors with public access modifiers provides visibility to the classes which can be instantiated.
2.     Define a protected or an internal constructor within an abstract class. Since a protected constructor allows the base class to do its own initialization when sub-classes are created and an internal constructor can be used to limit concrete implementations of the abstract class to the assembly which contains that class.
When to use
1.     Need to create multiple versions of your component since versioning is not a problem with abstract class. You can add properties or methods to an abstract class without breaking the code and all inheriting classes are automatically updated with the change.
2.     Need to to provide default behaviors as well as common behaviors that multiple derived classes can share and override.

Key Points
     1.We cannot create an object of Abstract Class but we can create a reference of it.
1.  using System;   
2.    
3.  namespace AbstractClassDemo   
4.  {   
5.      abstract class absClass { }   
6.      class Program   
7.      {   
8.          public static void Main(string[] args)   
9.          {   
10.             //We can't do this   
11.             //absClass cls = new absClass();   
12.             //We can do this   
13.             absClass cls;   
14.         }   
15.     }   
16. }   
  1. An inheritance between abstract to abstract classes is possible. We don't need to implement abstract methods of the base abstract class into a derived abstract class. We can implement it later in concrete classes.
1.  using System;   
2.    
3.  namespace AbstractClassDemo   
4.  {   
5.      abstract class absClassA   
6.      {   
7.          //Abstract Method   
8.          public abstract void SomeMethod();   
9.      }   
10.   
11.     abstract class absClassB: absClassA //Abstract to Abstract Inheritance   
12.     {   
13.     }   
14.   
15.     class Program: absClassB   
16.     {   
17.         public override void SomeMethod()   
18.         {   
19.             //Some Implementation Here   
20.         }   
21.   
22.         public static void Main(string[] args)   
23.         {   
24.         }   
25.     }   
26. }  
  1. An abstract class can never be sealed or static.
     
  2. An abstract class can have abstract as well as non abstract methods.
     
  3. The abstract keyword can be used with class, methods, properties, indexers and events.
     
  4. Abstract members can only be declared inside an abstract class.
     
  5. An abstract member cannot be static or private.
     
  6. An abstract method cannot be marked virtual.
     
  7. A concrete class cannot inherit more than one abstract class, in other words multiple Inheritance is not possible.
     
  8. Without an abstract class, we cannot implement the Template Method Pattern.


1. What is an abstract class?
An abstract class is a class that cannot be instantiated. It can only be used as a base class that is inherited by the derived classes. To make a class abstract, you will have to add the keyword “abstract” to the class. An abstract class will have abstract members like abstract methods, abstract events, abstract properties, etc. Abstract members are the members who don’t have any implementation, they just have the declaration. 
2. Do abstract classes have all the methods abstract?
No. An abstract class can have concrete methods too.
3. What is the use of abstract classes? Can’t we work without them?
Yes, we can work without abstract classes. But, we use abstract classes to avoid repetetion of code. If we have some common functionality that has to be implemented in more than two classes, then it is better to create an abstract class for that common functionality, rather than writing that common code again in all the classes. You just need to define an abstract class with all the common fields and methods. All the other classes will then inherit from the abstract class and can use those common fields and methods.
4. Where will you define the implementation of the abstract methods of the abstract class?
The classes derived from the abstract class must provide implementation of all the abstract methods. Otherwise, you’ll get a compile time error.
5. Can abstract classes have constructor?
Yes, abstract classes can have constructors.
6. Abstract classes cannot be instantiated. Then what is the use of constructor in an abstract class?
Like any other class constructor, an Abstract class constructor is used to initialize the fields in the abstract class. This constructor will get executed once the object of the derived class is created.
7. Whose constructor will be called first, abstract class constructor or the derived class constructor?
The abstract class constructor will be call first.
8. Can we use a sealed keyword with an abstract class?
No. Sealed classes cannot be inherited while an abstract class is meant to be inherited.
9. Can we have an abstract class derived from another abstract class?
Yes.




No comments:

Post a Comment