Saturday, March 1, 2014

Interfaces, Sealed Class & Abstract Class


This is the one of the most important features of C#. The .NET beginners may not understand the importance of the interfaces exactly, but in the real-time  development  of  .NET  applications,  the  interfaces  are  used  by  the  Sr.  Developers and the Jr. Developers. The "Interfaces" are similar to abstract classes, but having its own features along with having some differences with the abstract classes.

Features of Interfaces:
  • The interface can be declared with "interface" keyword, as follows:
              interface interfacename

  • The  interface  contains  only  method  declarations  and  can‘t  contain  method definitions (similar to the abstract methods).
  • The  interface  can‘t  contain  any  data  members  but  can  contain  "automatic properties"  (Already  you  know  that  the  automatic  property  doesn‘t  contain definitions for "get" and "set" accessors).
  • Interface methods are by default "public". You can‘t use another access modifier like private and public etc.
  • The interface can‘t be instantiated. That means you can‘t create an object for the interface.
  • The interface can‘t contain constructors.
  • The class that inherits the interface is called as Implementation Class.
  • The  implementation  class  should  implement  the  definitions  for  all  the  interface methods. If not, it would generate compile time errors.
  • One interface can be inherited by any no. of classes (Hierarchical inheritance).
  • One class can inherit any no. of interfaces (Multiple inheritance).
  • The  interface  methods  can‘t  be  declared  as  "virtual"  or  "static"  in  the  interface definition.

Implementation Syntax of Interfaces:

//interface definition

         interface interfacename


              returntype methodname(arguments);

//implementation class definition
         class classname : interfacename
              public returntype methodname(arguments)
                   //some code


 Sealed Classes

The sealed classes can be declared using "sealed" keyword as follows:
           sealed  class  classname
                //some members

  • The  sealed classes are just like normal classes, but you can‘t inherit the sealed class. In other words, you can‘t create a sub class for the sealed class.
  • Trying to inherit the sealed class would cause a compilation error.
  • This is just like "Final classes" in java.
  • Demo:
          sealed class one
          class two : one  //can‘t inherit this. Will give a compilation error.

 Abstract Classes and Methods

Abstract Methods
The abstract method is same as pure virtual functions in C++. The abstract method can be declared with "abstract" keyword like this:
  public abstract void samplemethod();
The  abstract  method  doesn‘t  contain  method  definition;  it  contains  only  method declaration as above.
The abstract methods can be declared only within abstract classes.

Abstract Classes
A class that is declared with "abstract" keyword is called as "abstract class".
     abstract class classname

  • If a class contains at least one abstract method, that class should be declared as abstract class.
  • The  abstract  class  can  contain  abstract  methods,  non-abstract  class  and  normal  data members also.
  • You can‘t create an object for the abstract class. It can be inherited from another non-abstract class.
  • The  non-abstract  class,  that  inherits  the  abstract  class,  should  implement  the definition(s)  (with "override"  keyword)  for  all  of  the  abstract  methods  declared  in  the abstract class.
Note:  The  access  modifiers  used  in  the  base  and  derived  classes  for  the  abstract methods should be             same.

   abstract class abstractclassname
          //data members if any
          //non-abstract methods if any
           accessmodifier abstract returntype methodname(arguments);
   class derivedclassname : abstractclassname
       accessmodifier override returntype methodname(arguments)
         //method body

 Abstract Class and Abstract Method

Download above code for Demo of Abstract Class and Methods : Download Links

Summary of Modifiers

  • Up to now, you learned several modifiers like static, new, virtual, abstract, override, sealed.
  • You get a summary of all of these modifiers with following table:
Abstract Method and Abstract Class