Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Method overloading and inheritance

Tags:

I have the following classes:

public class BaseRepository {     public virtual void Delete(int id)     {         Console.WriteLine("Delete by id in BaseRepository");     } }  public class EFRepository: BaseRepository {     public override void Delete(int id)     {         Console.WriteLine("Delete by Id in EFRepository");     }      public void Delete(object entity)     {         Console.WriteLine("Delete by entity in EFRepository");     } } 

Then I use it like:

var repository = new EFRepository(); int id = 1; repository.Delete(id); 

Why in that case only EFRepository.Delete(object entity) will call?

like image 687
Roman Marusyk Avatar asked Aug 30 '16 17:08

Roman Marusyk


People also ask

What is the difference between inheritance and method overloading?

Overloading allows several function definitions for the same name, distinguished primarily through different argument types; it is typically resolved at compile-time. Inheritance allows subclasses to define more special versions of the same function; it is typically resolved at run-time.

Is overloading related to inheritance?

Yes of course, overloading in inheritance class is possible in Java. Java compiler detect that add method has multiple implementations. so according to the parameter java compiler will determines which method has to be executed. class Parent { public void add(int a) { System.

What is the difference between method overriding and inheritance?

Inheritance enable us to define a class that takes all the functionality from parent class and allows us to add more. Method overriding occurs simply defining in the child class a method with the same name of a method in the parent class .

Can overloading a method replace inheritance?

Overloading is adding a different method with same name as existing one, that differs in input parameters and return type. It has nothing to do with inheritance.


1 Answers

Basically, the way method invocation works in C# is that the compiler looks at the most derived class first, and sees whether any newly declared methods (not including overrides) are applicable for the arguments for the call. If there's at least one applicable method, overload resolution works out which is the best one. If there isn't, it tries the base class, and so on.

I agree this is surprising - it's an attempt to counter the "brittle base class" issue, but I would personally prefer that any overridden methods were included in the candidate set.

Method invocation is described in section 7.6.5.1 of the C# 5 specification. The relevant parts here is:

  • The set of candidate methods is reduced to contain only methods from the most derived types: For each method C.F in the set, where C is the type in which the method F is declared, all methods declared in a base type of C are removed from the set. Furthermore, if C is a class type other than object, all methods declared in an interface type are removed from the set. (This latter rule only has affect when the method group was the result of a member lookup on a type parameter having an effective base class other than object and a non-empty effective interface set.)

And in the member lookup part of 7.4, override methods are explicitly removed:

Members that include an override modifier are excluded from the set.

like image 129
Jon Skeet Avatar answered Oct 20 '22 01:10

Jon Skeet