Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 571163
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T13:28:42+00:00 2026-05-13T13:28:42+00:00

I’m still trying to get a better understanding of Interfaces. I know about what

  • 0

I’m still trying to get a better understanding of Interfaces. I know about what they are and how to implement them in classes.

What I don’t understand is when you create a variable that is of one of your Interface types:

IMyInterface somevariable;

Why would you do this? I don’t understand how IMyInterface can be used like a class…for example to call methods, so:

somevariable.CallSomeMethod();

Why would you use an IMyInterface variable to do this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T13:28:42+00:00Added an answer on May 13, 2026 at 1:28 pm

    You are not creating an instance of the interface – you are creating an instance of something that implements the interface.

    The point of the interface is that it guarantees that what ever implements it will provide the methods declared within it.

    So now, using your example, you could have:

    MyNiftyClass : IMyInterface
    {
        public void CallSomeMethod()
        {
            //Do something nifty
        }
    }
    
    MyOddClass : IMyInterface
    {
        public void CallSomeMethod()
        {
            //Do something odd
        }
    }
    

    And now you have:

    IMyInterface nifty = new MyNiftyClass()
    IMyInterface odd = new MyOddClass()
    

    Calling the CallSomeMethod method will now do either something nifty or something odd, and this becomes particulary useful when you are passing in using IMyInterface as the type.

    public void ThisMethodShowsHowItWorks(IMyInterface someObject)
    {
        someObject.CallSomeMethod();
    }
    

    Now, depending on whether you call the above method with a nifty or an odd class, you get different behaviour.

    public void AnotherClass()
    {
        IMyInterface nifty = new MyNiftyClass()
        IMyInterface odd = new MyOddClass()
    
        // Pass in the nifty class to do something nifty
        this.ThisMethodShowsHowItWorks(nifty);
    
        // Pass in the odd class to do something odd
        this.ThisMethodShowsHowItWorks(odd);
    
    }
    

    EDIT

    This addresses what I think your intended question is – Why would you declare a variable to be of an interface type?

    That is, why use:

    IMyInterface foo = new MyConcreteClass();
    

    in preference to:

    MyConcreteClass foo = new MyConcreteClass();
    

    Hopefully it is clear why you would use the interface when declaring a method signature, but that leaves the question about locally scoped variables:

    public void AMethod()
    {
        // Why use this?
        IMyInterface foo = new MyConcreteClass();
    
        // Why not use this?
        MyConcreteClass bar = new MyConcreteClass();
    }
    

    Usually there is no technical reason why the interface is preferred. I usually use the interface because:

    • I typically inject dependencies so the polymorphism is needed
    • Using the interface clearly states my intent to only use members of the interface

    The one place where you would technically need the interface is where you are utilising the polymorphism, such as creating your variable using a factory or (as I say above) using dependency injection.

    Borrowing an example from itowlson, using concrete declaration you could not do this:

    public void AMethod(string input)
    {               
        IMyInterface foo;
    
        if (input == "nifty")
        {
            foo = new MyNiftyClass();
        }
        else
        {
            foo = new MyOddClass();
        }
        foo.CallSomeMethod();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
I don't have much knowledge about the IPv6 protocol, so sorry if the question
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I'm trying to select an H1 element which is the second-child in its group
Does anyone know how can I replace this 2 symbol below from the string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.