Object Oriented Programming/Design

...now browsing by category

 

good reference on abstract class vs interface

Friday, August 29th, 2014

This reading really makes it crystal clear about the similarity and difference between an abstract class and an interface http://msdn.microsoft.com/en-us/library/scsyfw1d(v=vs.71).aspx and it is easy to read. Here are the key points I took away from this casual reading:

  • Abstract class and interface both are to be inherited to be useful – this is one similarity.
  • The differences are many:
    • One subclass can inherit only one abstract class but can multiple interfaces or one base class + one or more interfaces.
    • Abstract class can have implementations but Interface does not have implementation.So if you want to have complete control of how a method is implemented, you build an abstract class to include it; otherwise, use interface to allow the inheriting class (subclass) to implement their own; in this sense, an abstract method in an abstract class is acting like a public method in an interface – they both must be implemented or overriden in a subclass.
    • An interface can be passed as an argument or initiated as an object such as:
       void GetEmailTemplate(ITemplate t) 
                     { //implementation ;
                     } 
      
       or  void SaveEmailTemplate()
      {
             IEmailTemplate t=new EmailTemplateClassThatInheritTheInterface(); 
             t.DoSomethingThatIsContractedToDoInTheInterface();
      
      
      }
      

      But an abstract class has no such freedom.

    • Interface has no knowledge and control of how its contract methods will be implemented; only thing for sure is all its public methods must be implemented in the subclass. If viewing the interface as a universally observed law – “crimes must be punished”, then while different countries must enforce the punishment but implementation of punishment can vary greatly, such as Singapore can canning you for spitting on street while other country just jails you even you murdered people, like in US.
  • So, in designing a solution, if I want to have some control over certain core operations of the classes, I would wrap those base methods with my desired implementations in an Abstract class so all the subclasses inheriting from it will automatically have my implementations, no kidding around.
  • And if I want to allow certain methods to be implemented in a subclass that I don’t give a damn, then I will add a few virtual methods in my abstract class – that give me some of those Interface power and flexibility; virtual can have some basic implementation for subclass to override with its own implementation or use directly without second thought.
  • Another concept is an Abstract method, what about that? Well, think this way, you want to force a subclass that inherits your abstract class to behave like inheriting from an interface (but you don’t want to use an interface because you want to have a tight grip on certain base implementations), what will you do? Yes, an abstract method – there is no implementation in base class but subclass must override it with its own implementation.