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

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?
LVL 63
Julian HansenAsked:
Who is Participating?
 
käµfm³d 👽Connect With a Mentor 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.
0
 
rajshekherConnect With a Mentor 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.
0
 
AndyAinscowConnect With a Mentor Freelance programmer / ConsultantCommented:
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

0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
Julian HansenAuthor 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?
0
 
AndyAinscowFreelance programmer / ConsultantCommented:
Are you certain that is exactly what compiles in the other app ?  (I suspect it isn't)
0
 
Julian HansenAuthor 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
0
 
Julian HansenAuthor 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.
0
 
Julian HansenAuthor 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.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.