Wednesday, February 25, 2015

Some more about Abstract class and Interface

 Astract 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.
    ******************************Abstract class ends here*****************
An interface acts as a contract between itself and any class or struct which implements it. It means a class that implement an interface is bound to implement all its members. Interface has only member’s declaration or signature and implicitly every member of an interface is public and abstract.
For example, the most common use of interfaces is, within SOA (Service Oriented Architecture). In SOA (WCF), a service is exposed through interfaces to different clients. Typically, an interface is exposed to a group of clients which needs to use common functionalities.

  1. interface IStore

  2. {

  3. void Read();

  4. void Write();

  5. }

  6.  

  7. interface ICompress

  8. {

  9. void Compress();

  10. void Decompress();

  11. }

  12.  

  13. public class Document : IStore, ICompress

  14. {

  15. #region IStore

  16.  

  17. public void Read()

  18. {

  19. Console.WriteLine("Executing Document's Read Method for IStore");

  20. }

  21.  

  22. public void Write()

  23. {

  24. Console.WriteLine("Executing Document's Write Method for IStore");

  25. }

  26.  

  27. #endregion // IStore

  28.  

  29. #region ICompress

  30.  

  31. public void Compress()

  32. {

  33. Console.WriteLine("Executing Document's Compress Method for ICompress");

  34. }

  35. public void Decompress()

  36. {

  37. Console.WriteLine("Executing Document's Decompress Method for ICompress");

  38. }

  39. #endregion // ICompress

  40. }


Features of Interface

  1. An interface doesn't provide inheritance like a class or abstract class but it only declare members which an implementing class need to be implement.
  2. It cannot be instantiated but it can be referenced by the class object which implements it. Also, Interface reference works just like object reference and behave like as object.

    1. IStore IObjStore = new Document();

    2. ICompress IObjCompress = new Document();


  3. It contains only properties, indexers, methods, delegates and events signature.
  4. It cannot contains constants members, constructors, instance variables, destructors, static members or nested interfaces.
  5. Members of an interface cannot have any access modifiers even public.
  6. Implicitly, every member of an interface is public and abstract. Also, you are not allowed to specify the members of an interface public and abstract or virtual.
  7. An interface can be inherited from one or more interfaces.
  8. An interface can extend another interface.
  9. A class or struct can implements more than one interfaces.
  10. A class that implements an interface can mark any method of the interface as virtual and this method can be overridden by derived classes.
  11. Implementing multiple interfaces by a class, sometimes result in a conflict between member signatures. You can resolve such conflicts by explicitly implementing an interface member.

    1. interface IStore

    2. {

    3. void Read();

    4. void Write();

    5. }

    6.  

    7. interface ICompress

    8. {

    9. void Compress();

    10. void Decompress();

    11. }

    12.  

    13. public class Document : IStore, ICompress

    14. {

    15. #region IStore Explicit Implementation

    16.  

    17. public void IStore.Read()

    18. {

    19. Console.WriteLine("Executing Document's Read Method for IStore");

    20. }

    21.  

    22. public void IStore.Write()

    23. {

    24. Console.WriteLine("Executing Document's Write Method for IStore");

    25. }

    26.  

    27. #endregion // IStore

    28.  

    29. #region ICompress Implicit Implementation

    30.  

    31. public void Compress()

    32. {

    33. Console.WriteLine("Executing Document's Compress Method for ICompress");

    34. }

    35. public void Decompress()

    36. {

    37. Console.WriteLine("Executing Document's Decompress Method for ICompress");

    38. }

    39. #endregion // ICompress

    40. }


  12. It is a good practice to start all interface names with a capital “I” letter.

Common design guidelines for Interface

  1. Keep your interfaces focused on the problem you are trying to solve and keep related tasks (methods) into a interface. Interfaces that have multiple unrelated tasks tend to be very difficult to implement in a class. Split up interfaces that contain unrelated functionality.
  2. Make sure your interface does not contain too many methods. Since too many methods makes implementing the interface difficult as the implementing class has to implement each and every method in the interface.
  3. Don't make interfaces for specific functionality. An interface should define the common functionality that can be implemented by the classes of different modules or subsystems.

When to use

  1. Need to provide common functionality to unrelated classes.
  2. Need to group objects based on common behaviors.
  3. Need to introduce polymorphic behavior to classes since a class can implements more than one interfaces.
  4. Need to provide more abstract view to a model which is unchangeable.
  5. Need to create loosely coupled components, easily maintainable and pluggable components (like log4net framework for logging) because implementation of an interface is separated from itself.

Disadvantage of Interface

  1. The main issue with an interface is that when you add a new members to its, then you must implement those members within all of the classes which implement that interface.
  2. Interfaces are slow as these required extra in-direction to find corresponding method in in the actual class.

 Now over to you:
"A blog is nothing without reader's feedback and comments. So please provide your valuable feedback so that i can make this blog better and If you like my work, you can appreciate by leaving your comments. Stay tuned and stay connected for more technical updates."

No comments:

Post a Comment