Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we call method in implementation class not defined in its interface using interface variable?

Tags:

c#

oop

Is it possible to call a method in implementation class of an interface which is not defined in interface using interface variable like below:

interface ILookup {
            public void someInterfaceMethod1();
            public void someInterfaceMethod2();
}

...and implementation class:

public class extendedLookUpImplementor: ILookup
{
          //constructor
          LookUpImplementor(){
          }

          public void someInterfaceMethod1(){
           // Implementation Code here.
          }

          public void someInterfaceMethod2(){
           // Implementation Code here.
          }

          public void ExtendedMethod(){
           // Implementation Code here.
          }

}

In client code:

ILookup lookupVar = new LookUpImplementor();
lookupVar -> someInterfaceMethod1(); // i know it will work.
lookupVar -> someInterfaceMethod2(); // i know it will work.

My question is, can i call ExtendedMethod using lookupVar like below:

lookupVar -> ExtendedMethod(); // Note again that ExtendedMethod() is not defined in Ilookup interface/contract.
like image 651
Fakhar Anwar Avatar asked Oct 21 '22 01:10

Fakhar Anwar


1 Answers

Only by casting lookupVar as extendedLookUpImplementor, or by reflection I think.

like image 88
Ian1971 Avatar answered Oct 23 '22 18:10

Ian1971