Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. what is advantges of using interface reference instead of class refernce

what is advantges of using interface reference instead of class refernce

Scheduled Pinned Locked Moved C#
databasequestion
9 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    am 2009
    wrote on last edited by
    #1

    Hi I have one query as Consider i have ICheck interface which is implemented by class "Data". So I can access functions in Class Data using Code as 1) Data obj; obj.Fun(); 2) ICheck ck = new Data(); ck.Fun() So what is advantages of using second code.

    D OriginalGriffO P P P 6 Replies Last reply
    0
    • A am 2009

      Hi I have one query as Consider i have ICheck interface which is implemented by class "Data". So I can access functions in Class Data using Code as 1) Data obj; obj.Fun(); 2) ICheck ck = new Data(); ck.Fun() So what is advantages of using second code.

      D Offline
      D Offline
      DaveyM69
      wrote on last edited by
      #2

      You can implement the ICheck interface in any class/struct you like. You can then use the same method an any instances of any of those classes.

      Dave
      Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
      BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

      1 Reply Last reply
      0
      • A am 2009

        Hi I have one query as Consider i have ICheck interface which is implemented by class "Data". So I can access functions in Class Data using Code as 1) Data obj; obj.Fun(); 2) ICheck ck = new Data(); ck.Fun() So what is advantages of using second code.

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        You can only derive a class from a single other class. For example

        public partial class frmMain : Form
            {
            ...
            }
        

        But you can inherit as many Interfaces as you need. For example

        public partial class frmMain : Form, ICloneable, IComparable, IDisposable
            {
            ...
            }
        

        (Not that I am suggesting that is a sensible Interface list for a form, it's just an example)

        Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        1 Reply Last reply
        0
        • A am 2009

          Hi I have one query as Consider i have ICheck interface which is implemented by class "Data". So I can access functions in Class Data using Code as 1) Data obj; obj.Fun(); 2) ICheck ck = new Data(); ck.Fun() So what is advantages of using second code.

          P Offline
          P Offline
          Pete OHanlon
          wrote on last edited by
          #4

          In your example, there's very little advantage. Where the use of the interface comes into its own is when you want to perform a specific method, e.g. Dispose an object. Rather than having to use reflection to determine whether or not an object contains that method, you can use the interface to do the action. Consider the following snippet:

          Data obj = new Data();
          ICheck check = obj as ICheck;
          if (check != null)
          check.Fun();

          Now, if Data doesn't implement this interface, the code still compiles and behaves as you'd expect. This really comes into its own if you are using something like Inversion of Control where you'd use the interface to retrieve the object, e.g.:

          ICheck obj = MyIocContainer.Resolve(/* some reference to the Data class);
          obj.Fun();

          I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

          Forgive your enemies - it messes with their heads

          My blog | My articles | MoXAML PowerToys | Onyx

          modified on Saturday, September 11, 2010 8:14 AM

          P 1 Reply Last reply
          0
          • A am 2009

            Hi I have one query as Consider i have ICheck interface which is implemented by class "Data". So I can access functions in Class Data using Code as 1) Data obj; obj.Fun(); 2) ICheck ck = new Data(); ck.Fun() So what is advantages of using second code.

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            Very little with a local variable -- very much with a parameter or property.

            1 Reply Last reply
            0
            • A am 2009

              Hi I have one query as Consider i have ICheck interface which is implemented by class "Data". So I can access functions in Class Data using Code as 1) Data obj; obj.Fun(); 2) ICheck ck = new Data(); ck.Fun() So what is advantages of using second code.

              P Offline
              P Offline
              Pete OHanlon
              wrote on last edited by
              #6

              An important one - the using () pattern wouldn't work without the use of IDisposable.

              I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

              Forgive your enemies - it messes with their heads

              My blog | My articles | MoXAML PowerToys | Onyx

              1 Reply Last reply
              0
              • P Pete OHanlon

                In your example, there's very little advantage. Where the use of the interface comes into its own is when you want to perform a specific method, e.g. Dispose an object. Rather than having to use reflection to determine whether or not an object contains that method, you can use the interface to do the action. Consider the following snippet:

                Data obj = new Data();
                ICheck check = obj as ICheck;
                if (check != null)
                check.Fun();

                Now, if Data doesn't implement this interface, the code still compiles and behaves as you'd expect. This really comes into its own if you are using something like Inversion of Control where you'd use the interface to retrieve the object, e.g.:

                ICheck obj = MyIocContainer.Resolve(/* some reference to the Data class);
                obj.Fun();

                I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

                Forgive your enemies - it messes with their heads

                My blog | My articles | MoXAML PowerToys | Onyx

                modified on Saturday, September 11, 2010 8:14 AM

                P Offline
                P Offline
                Paul Michalik
                wrote on last edited by
                #7

                Pete O'Hanlon wrote:

                Now, if Data doesn't implement this interface, the code still compiles and behaves as you'd expect.

                Oh my god, no! If Data does not implement ICheck the code would not compile, at least in the statically typed polymorphic world!

                P 1 Reply Last reply
                0
                • A am 2009

                  Hi I have one query as Consider i have ICheck interface which is implemented by class "Data". So I can access functions in Class Data using Code as 1) Data obj; obj.Fun(); 2) ICheck ck = new Data(); ck.Fun() So what is advantages of using second code.

                  P Offline
                  P Offline
                  Paul Michalik
                  wrote on last edited by
                  #8

                  Go and google for Dependency inversion. Basically, you use interfaces to loosen dependencies between components. Consider:

                  public class Data : IData {
                  private IDataChecker mDataChecker;

                  //...

                  public Data(IDataChecker pDataChecker) {
                  mDataChecker = pDataChecker;
                  }

                  //...
                  }

                  Your concrete Data depends only on IDataChecker interface, not on its implementation. The consequence is that you can use many different implementations of IDataChecker without changing Data.

                  1 Reply Last reply
                  0
                  • P Paul Michalik

                    Pete O'Hanlon wrote:

                    Now, if Data doesn't implement this interface, the code still compiles and behaves as you'd expect.

                    Oh my god, no! If Data does not implement ICheck the code would not compile, at least in the statically typed polymorphic world!

                    P Offline
                    P Offline
                    Pete OHanlon
                    wrote on last edited by
                    #9

                    I checked the code I typed in and realised I'd forgotten to put as ICheck on the relevant line. I've amended it, and the code works exactly as I described. Thanks for bringing this to my attention.

                    I have CDO, it's OCD with the letters in the right order; just as they ruddy well should be

                    Forgive your enemies - it messes with their heads

                    My blog | My articles | MoXAML PowerToys | Onyx

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups