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 8956069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:42:17+00:00 2026-06-15T14:42:17+00:00

I know interface cannot be instantiated, but if we assign it to an object,

  • 0

I know interface cannot be instantiated, but if we assign it to an object, could anyone please explain how the memory gets allocated to it. For ex:

ITest obj = (ITest) new TestClass1();  //TestClass1 is a class which implements ITest
obj.MethodsDefinedInInterface();

Does ITest converts to object to save properties and methods of TestClass1.

  • 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-06-15T14:42:18+00:00Added an answer on June 15, 2026 at 2:42 pm

    I’m not sure exactly what you mean by ‘allocation’. The following statement makes two seperate ‘allocations’:

    TestClass1 test = new TestClass1();
    

    First is the new TestClass1() statement which allocates sizeof(TestClass1) on the heap. Second, the assignment of the address of the heap allocation is stored in the variable test, which is allocated on the stack as sizeof(object *) (i.e. IntPtr.Size, or 32/64 bits based on the hardware+OS+software running).

    The following statement is EXACTLY the same in ‘allocations’:

    ITest test = new TestClass1();
    

    The only difference between the two is the methods available to be called on the variable test.

    Note: This is NOT true with a structure that implements an interface. Interfaces must be a reference type, and as you know, structs are not. This is called boxing in .NET and allows a struct to be referenced as if it were a reference type by first placing a copy of the struct on the heap.

    So we now re-evaluate the statement:

    TestSTRUCT1 test2 = new TestSTRUCT1();
    

    This ‘allocates’ sizeof(TestSTRUCT1) on the stack at the named variable test2. (Not sure what the impact of the assignment to new TestSTRUCT1() is, it may create an additional stack copy but that should be removed immediately after the assignment.

    If we then assign this value to an interface:

    ITest test3 = test2;
    

    We have now made two more allocations. First the structure is copied to the heap. Then the address of that heap-resident structure is placed in a newly ‘allocated’ variable test3 (on the stack).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Anyone know of a good book on Game Interface Design (not game play mechanics;
I know that you can't have a constructor in an interface, but here is
I want to know the in-memory representation of .NET constructs such as interface ,
I have tried google and looked at the iOS Human Interface Guidelines but cannot
Possible Duplicate: Interface defining a constructor signature? I know that you cannot specify a
I know this error relates to missing TAB, but I somehow cannot find out
I know this is very often asked , but I cannot find a working
I'm learning Java and as I know only abstract classes and interfaces cannot be
I am still not so familiar with interfaces in Java. I know that interface
I know that an interface does not have a body, just a method definition.

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.