Skip to main content

Features of Interface

Features of Interface


    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.

    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.

        IStore IObjStore = new Document();
        ICompress IObjCompress = new Document();


    It contains only properties, indexers, methods, delegates and events signature.

    It cannot contains constants members, constructors, instance variables, destructors, static members or nested interfaces.

    Members of an interface cannot have any access modifiers even public.

    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.

    An interface can be inherited from one or more interfaces.

    An interface can extend another interface.

    A class or struct can implements more than one interfaces.

    A class that implements an interface can mark any method of the interface as virtual and this method can be overridden by derived classes.

    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.

        interface IStore
        {
        void Read();
        void Write();
        }
        
        interface ICompress
        {
        void Compress();
        void Decompress();
        }
        
        public class Document : IStore, ICompress
        {
        #region IStore Explicit Implementation
        
        public void IStore.Read()
        {
        Console.WriteLine("Executing Document's Read Method for IStore");
        }
        
        public void IStore.Write()
        {
        Console.WriteLine("Executing Document's Write Method for IStore");
        }
        
        #endregion // IStore
        
        #region ICompress Implicit Implementation
        
        public void Compress()
        {
        Console.WriteLine("Executing Document's Compress Method for ICompress");
        }
        public void Decompress()
        {
        Console.WriteLine("Executing Document's Decompress Method for ICompress");
        }
        #endregion // ICompress
        }

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

Common design guidelines for Interface


    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.

    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.

    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


    Need to provide common functionality to unrelated classes.

    Need to group objects based on common behaviors.

    Need to introduce polymorphic behavior to classes since a class can implements more than one interfaces.

    Need to provide more abstract view to a model which is unchangeable.

    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


    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.

    Interfaces are slow as these required extra in-direction to find corresponding method in in the actual class.

Comments

Popular posts from this blog

NHibernate QueryOver Class And Projection....

Introduction The ICriteria API is NHibernate's implementation of Query Object . NHibernate 3.0 introduces the QueryOver api, which combines the use of Extension Methods and Lambda Expressions (both new in .Net 3.5) to provide a statically typesafe wrapper round the ICriteria API. QueryOver uses Lambda Expressions to provide some extra syntax to remove the 'magic strings' from your ICriteria queries. So, for example: .Add(Expression.Eq("Name", "Smith")) becomes: .Where<Person>(p => p.Name == "Smith") With this kind of syntax there are no 'magic strings', and refactoring tools like 'Find All References', and 'Refactor->Rename' work perfectly. Note: QueryOver is intended to remove the references to 'magic strings' from the ICriteria API while maintaining it's opaqueness. It is not a LINQ provider; NHibernate 3.0 has a built-in ...

Passing Data from View to Controller Using Ajax Example Jquery

Jquery       $ ( '#btnSaveComments' ). click ( function () { var comments = $ ( '#txtComments' ). val (); var selectedId = $ ( '#hdnSelectedId' ). val (); $ . ajax ({ url : '<%: Url.Action("SaveComments")%>' , data : { 'id' : selectedId , 'comments' : comments }, type : "post" , cache : false , success : function ( savingStatus ) { $ ( "#hdnOrigComments" ). val ( $ ( '#txtComments' ). val ()); $ ( '#lblCommentsNotification' ). text ( savingStatus ); }, error : function ( xhr , ajaxOptions , thrownError ) { $ ( '#lblCommentsNotification' ). text ( "Error encountered while saving the comments." ); } }); });     Controller    [ HttpPost ] public ActionResult SaveComments ( int id , string com...

Understanding machine learning

What is machine learning does?  It Finds patterns in data and uses those patterns to predict the future.          EXAMPLES :  Detecting credit card fraud : You can use machine learning  detect credit card fraud, Suppose you have data about previous credit card transactions. You could find patterns in that data potentially  that allows you to detect when a new credit card transaction is likely to be fraudulent. Determining whether a customer is likely to switch to a competitor :  You could possibly find the patterns in the existing customer data that will help you do that Lots more What does it mean to learn ? Yes it's something like your own learning process. You have learned reading,listening, walking everything by using your experience on it, the data you have in your mind and continuous improvements.  How did you learn to read? Well learning requires identifying patterns. Reading for instance you ...