title: base class implementing base interface while derived/concrete class implementing extended interface, why?
body: <p>I am following a book namely ".NET Domain Driven Design with C#".</p>
  
  <p>Question is based on scenario as shown in Class Diagram below:</p>
  
  <p>Figure:
  <a href="http://screencast.com/t/a9UULJVW0" rel="nofollow">http://screencast.com/t/a9UULJVW0</a></p>
  
  <p>In this diagram, </p>
  
  <p>A) IRepository interface is implemented by (abstract base class) RepositoryBase  whereas,</p>
  
  <p>B) IRepository interface is also extended by interface ICompanyRepository (ICompanyRepository <strong>:</strong> IRepository).</p>
  
  <p>C) ICompanyRepository is implemented by CompanyRepository which is derived from SQLRepositoryBase which is derived from RepositoryBase (; which, as stated in point A), implements IRepository which is parent if ICompanyRepository). </p>
  
  <p>D) i create a variable of interface ICompanyRepository having reference to object of clas  CompanyRepository, like below:</p>
  
  <pre><code>ICompanyRepository comRep = new Company Repository();
  </code></pre>
  
  <p>Now, if i call Add() function with ICompanyRepository variable comRep...</p>
  
  <pre><code>comRep.Add(); 
  </code></pre>
  
  <p>then Add() function in RepositoryBase class (which is parent of CompanyRepository) is called.</p>
  
  <p><strong>My Question:</strong>
  What is the exact underlying Object Oriented Rule/Mechanism takes place due to which the function Add()  in (abstract-base) class "RepositoryBase" is called? For convenience i am stating below two possible mechanisms: (please tell me which one of two stated below is correct underlying mechanism)</p>
  
  <p><strong>Mechanism-1</strong>
  Is this Add() function in base class "RepositoryBase" is called because "RepositoryBase" implements IRepoitory ? (Hence making it mandatory for RepositoryBase class to implement IRepository in order to call Add() )</p>
  
  <p><strong><em>OR</em></strong></p>
  
  <p><strong>Mechanism-2:</strong> 
  The Add() function in base class "RepositoryBase" is called because CompanyRepository implements ICompanyRepository which implements IRepository which contains definition for Add() function such that when Add() function is called on (with variable of) ICompanyRepository then it first finds the definition of Add in ICompanyRepository and then in parent interface IRepository and then jumps to CompanyRepository class to find implementation of Add() function and not finding the definition of Add() function it traverse upward to parent class SQLRepositoryBase to find Add() function and so on, and as it finds the function Add() in RepositoryBase class so it calls the Add() function in RepositoryBase. This means that if it would have found the Add() function in any of derived classes of RepositoryBase, it had not traverse further upward (in parent class). All of this also means that , in order to traverse from derived class to parent class in chain of classes just to find Add() function, the RepositoryBase class does not really need to inherit directly from IRepository?</p>
  
  <hr>
  
  <p>There are additional thing in my question and i am unable to understand which OO-Rule is applying in my case as stated below:</p>
  
  <p>In my Question there are two Interfaces one is parent namely IRepository and other is extending it namely ICompanyRepository. Parent interface IRepository contains deifinition of Add() function but not the child interface ICopmanyRepository.</p>
  
  <p>Last derived class in the chain of class Hierarchy  "CompanyRepository" implements ICompanyRepository (CompanyRepository does not implement Add() function of IRepository interface) whereas root (top most parent) (abstract base) class  namely RepositoryBase implements the Add() function.</p>
  
  <p>So the structure is like the image shown in <a href="http://screencast.com/t/a9UULJVW0" rel="nofollow">http://screencast.com/t/a9UULJVW0</a>.</p>
  
  <p>Now if i call Add() function:</p>
  
  <p><code>code</code>ICompanyRepository lastDerived = new CompanyRepository(); 
  ICompanyRepository->Add();<code>code</code></p>
  
  <p>Then according to the OO-Rule you stated in you answer, the lookup will start from CompanyRepository class with expectation that CompanyRepository would have implemented Add() function as 
  <code>code</code> IRepository.Add() {
  } //deduced from P17 and P18 in [link] <a href="http://www.codeproject.com/Articles/18743/Interfaces-in-C-For-Beginners[link]" rel="nofollow">http://www.codeproject.com/Articles/18743/Interfaces-in-C-For-Beginners[link]</a><code>code</code></p>
  
  <p>BUT, in my case class CompanyRepository does not have implementation IRepository.Add() { } although the flow of control (while tracing) jumps to Add() function in base class successfully (And code is working fine). I am unable to understand which OO-Rule is applying here ? </p>
  
  <p>Please let me know if you need me to show above scenario with code.</p>