We help IT Professionals succeed at work.

Can a class that implments an Interface declare a member that differs from the Interface definition?

Medium Priority
747 Views
Last Modified: 2012-08-12
I thought the answer to this question was no

public interface Iface
{
   void func1(string name);
   void func2(string name, string surname);
}

public class MyClass : IFace
{
  public void func1(string name, int age);
  public void func2(string name, string surname, string address);
}

Open in new window


Is the above leagal or does the class have to declare members that exactly match the Interface definition?
Comment
Watch Question

Raj ShekharSr. Tech Arch.
Commented:
Hi,

Yes, because, the declared class have different parametered function. then it is considered that different function through function overloading.

But you must to implement the Interface function in the declared class MyClass.
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT
Commented:
No.  It isn't complete

public interface Iface
{
   void func1(string name);
   void func2(string name, string surname);
}

public class MyClass : IFace
{
  public void func1(string name);  //MUST implement
  public void func2(string name, string surname);  //MUST implement

  public void func1(string name, int age);
  public void func2(string name, string surname, string address);
}
                                  

Open in new window

CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Author

Commented:
Ok, that is what I thought - but here is my problem.

I have inherited a project from another developer - based on NopCommerce.

The original source from NopCommerce contains two files
INopService.cs
NopService.cs

Extracts to follow

INopService
public interface INopService
{
    ...
    void UpdateOrderBillingInfo(int orderId, string firstName, string lastName, string phone, string email, string fax, string company, string address1, string address2, string city, string region, string country, string postalCode, string usernameOrEmail, string userPassword);
    
    void UpdateOrderShippingInfo(int orderId, string firstName, string lastName, string phone, string email, string fax, string company, string address1, string address2, string city, string region, string country, string postalCode, string usernameOrEmail, string userPassword);
    ...
}

Open in new window


NopService
public class NopService : INopService
{
   ...
   public void UpdateOrderBillingInfo(int orderId, string firstName, string lastName, string phone, string email, string fax, string company, string address1, string address2,
            string suburb, string city, string stateProvinceAbbreviation, string countryThreeLetterIsoCode, string postalCode, string usernameOrEmail, string userPassword)

   public void UpdateOrderShippingInfo(int orderId, string firstName, string lastName, string phone, string email, string fax, string company, string address1, string address2,
            string suburb, string city, string stateProvinceAbbreviation, string countryThreeLetterIsoCode, string postalCode, string usernameOrEmail, string userPassword)

}

Open in new window


This code compiles fine - but the same two files in my source don't and generate the error that a member does not exist in the class implementing the interface.

My question then is how can the above compile with the Nop Commerce release?
AndyAinscowFreelance programmer / Consultant
CERTIFIED EXPERT

Commented:
Are you certain that is exactly what compiles in the other app ?  (I suspect it isn't)
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Author

Commented:
This is why I am posting here - did a forum post at NC but no responses.

I just checked and I posted the code from my version of the source instead of the original NP code. Here is the code from their files NCC denotes NopCommerce Class and NCI denotes the interface. I have also attached the original files.

As you can see they are still different.

I searched the source for all instances of UpdateOrder and only these were found.

Just confused as to why the downloaded source for NC 2.6 compiles?


NCI void UpdateOrderBillingInfo(int orderId, string firstName, string lastName, string phone, string email, string fax, string company, string address1, string address2, string city, string region, string country, string postalCode, string usernameOrEmail, string userPassword);
NCC void UpdateOrderBillingInfo(int orderId, string firstName, string lastName, string phone, string email, string fax, string company, string address1, string address2, string city, string stateProvinceAbbreviation, string countryThreeLetterIsoCode, string postalCode, string usernameOrEmail, string userPassword)

NCI void UpdateOrderShippingInfo(int orderId, string firstName, string lastName, string phone, string email, string fax, string company, string address1, string address2, string city, string region, string country, string postalCode, string usernameOrEmail, string userPassword);

NCC void UpdateOrderShippingInfo(int orderId, string firstName, string lastName, string phone, string email, string fax, string company, string address1, string address2, string city, string stateProvinceAbbreviation, string countryThreeLetterIsoCode, string postalCode, string usernameOrEmail, string userPassword)

Open in new window

NopService.cs
INopService.cs
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015
Commented:
The NC 2.6 code you just posted will compile because the function signatures match. Remember that the compiler doesn't care one bit about the names of parameters. All it cares about is the name of your method, the number of parameters, and the types of each parameter. This is how the compiler determines which version of an overload to use when it is compiling your code.

In your case, the NC 2.6 class correctly defines two methods which both correspond to a method definition in the interface with the appropriate name, number of parameters, and parameter types. It does not matter that the interface says "region" where the class says "stateProvinceAbbreviation"--both are the same type:  string. If the class instead had defined "stateProvinceAbbreviation" to be of type int, for example, then you would have a problem.
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Author

Commented:
@kaufmed - I have been looking at this code for a week and I missed that.

Sometimes it takes a fresh pair of eyes. I thought I had lost my marbles - re-read the .Net doc's on Interfaces trying to see if I had missed something.

Thank you.
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2019

Author

Commented:
All three of you assisted in helping solve this issue for me. kaufmed pointed me in the right direction to understand what was going on.

Thanks to all three of you - really appreciate your input.

Explore More ContentExplore courses, solutions, and other research materials related to this topic.