Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
We all know Interfaces are not classes and so interfaces do not inherit any class and even do not inherit object class.

But when we Implement a interface in a class and override the object class method in that class, then we can call that object class method overridden in our child class from the interface(by interface reference variable).

How can interface call personal methods of child class?

Answer Please..........

C#
IMyInterface
{
   void ShowMe();
}
 
ClassA : IMyInterface
{
  void ShowMe()
  {
    MessageBox.Show("ClassA");
  }
  void Display()
  {
    MessageBox.Show("Display");
  }

  string ToString()
  {
    MessageBox.Show("ToString Class");
    return "";
  }
  
}
 
//Now, if the call is made like:


IMyInterface objClass = new ClassA();


objClass.ShowMe();

//ShowMe() method can be called because it is overridden method of the interface.

objClass.Display();

// Display() method can't be called because it is a personal method of ClassA.

objClass.ToString()

//when we call ToString() method, it runs.
//How toString() Method runs while it is not overridden method of interface.How can interface call personal methods of ClassA.

//I found some answer while googling that ToString is a method of object class that's why it founds in every class and interfaces.

//while more searching i found that interfaces do not inherit any class even interfaces can't inherit any class(it's a rule,interfaces are not classes and they don't inherit any class).

//Then how ToString runs???????????
Posted
Updated 18-Jul-12 2:36am
v2

I am not sure why you care, the fact is, it works, and it's the compilers problem how. My guess is that it passes an instance of the class, but only exposes the methods on the interface.
 
Share this answer
 
I guess you are speaking of such implementation:
C#
IMyInterface
{
   void ShowMe();
}

ClassA : IMyInterface
{
  void ShowMe()
  {
    MessageBox.Show("ClassA");
  }
}

ClassB : IMyInterface
{
  void ShowMe()
  {
    MessageBox.Show("ClassB");
  }
}

Now, if the call is made like:
C#
IMyInterface objClass = new ClassA();
objClass.ShowMe();
// ClassA will be shown.

This is an example of how Interface object was used to call method of child class. In actual it was not interface, it was ClassA object only. Because of Inheritance, child class object can be assigned to base class. Following implementation helps in cases where we need to design things whose implementation will be configured for a particular workflow. Generally Factory pattern is based on such implementation.

In actual it is the Child class object that is at work and not interface object. It deals with the problem of creating objects without specifying the exact class of object that will be created.
The essence here is to define an interface for creating an object, but let the classes that implement the interface decide which class to instantiate.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900