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

The Core Concepts of Angular -- Jithin CJ

I started to learn angular from 2016, I was very curious about the celibacy of this super hero. From my initial understanding is like, the power of angular is only limited on " html decoration "  But this JavaScript framework has the potential to re-define conventional html-css patterns . Modern browsers support for things like modules, classes, lambdas, generators, etc. These features fundamentally transform the JavaScript programming experience. But big changes aren't constrained merely to JavaScript. Web Components are on the horizon. The term Web Components usually refers to a collection of four related W3C specifications: Custom Elements - Enables the extension of HTML through custom tags.  HTML Imports - Enables packaging of various resources (HTML, CSS, JS, etc.).  Template Element - Enables the inclusion of inert HTML in a document.  Shadow DOM - Enables encapsulation of DOM and CSS.  Developers can create fully encapsulated (Shadow DOM) declar