Click here to Skip to main content
15,882,114 members
Please Sign up or sign in to vote.
3.50/5 (2 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
Comments
Sergey Alexandrovich Kryukov 19-Jul-12 0:58am    
What do you call a "personal method"? :-)
--SA
Prasad_Kulkarni 19-Jul-12 0:59am    
You've already ask'd this question:
How Interfaces are calling Object class methods?[^]
How Interfaces are calling Object class methods?[^]
Don't re-post your questions, If you want to modify or update your question then use 'Improve question' widget. But don't re-post it.
Sergey Alexandrovich Kryukov 19-Jul-12 1:35am    
Thank you for pointing it out, I did not know.
The question is incorrect, but not so trivial. I provided my answer, please see -- this is interesting enough.
--SA
Prasad_Kulkarni 19-Jul-12 4:46am    
You're welcome SA!
Sebastian T Xavier 19-Jul-12 1:15am    
Don't re-post

The question is based on some misunderstanding. All types inherit from System.Object, but this inheritance is with a twist. When you call a method of System.Object, such as System.Object.ToString, and the target object is of the value type, the object is boxed, and the boxed representation of it does inherit from System.Object. Would you be surprised of the code shown below?
C#
int value = 3;
string stringValue = value.ToString(); //where ToString is System.Object

Here is what happens here: a compiler interprets the method call as boxing, followed by the call:
C#
//pseudo-code:
class ValueType : System.Object {/*...*/}
class Int32 : System.ValueType {/*...*/}
string stringValue = ((System.Int32)value).ToString();


That was just a warm-up exercise, to feel more comfortable with types and realize that things are not so trivial.

Now, let's come back to interfaces. You should remember that is some variable is declared as an interface type, it can only be a compile-time type, because interfaces cannot be instantiated. When this variable is instantiated, its run-time type is always some class or structure (structures also can implement interfaces).

Compiler "knows" that this run-time type is some class or some structure, but it "does not know" what exactly. But as this unknown type is always derived from System.Object, the compiler "knows" that the variable is assignment compatible with is, so its methods can be called with this variable, which is passed to the method as hidden "this" parameter.

To say it shorter: even though an interface is not inherited from System.Object, the initialized variables of an interface type always have some run-time type which does.

—SA
 
Share this answer
 
v3
Comments
Prasad_Kulkarni 19-Jul-12 4:46am    
Yepp. Good answer indeed +5!
Sergey Alexandrovich Kryukov 19-Jul-12 11:15am    
Thank you, Prasad.
--SA
I think your example is very bad: because what you should do in your class is override the ToString method it inherited from System.Object , not replace it (what you did, and you did it wrong - look at the compiler warning (you should use the new keyword on the method declaration)).
So implicit your ClassA is defined like this
C#
class ClassA : System.Object, IMyInterface
.
There for it has a ToString method you can call (btw. it won't call yours!!!!)
Try for yourself:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CallInterface
{
    interface IMyInterface
    {
        void ShowMe();
    }

    class ClassA : IMyInterface
    {
        public void ShowMe()
        {
            Console.WriteLine("ClassA");
        }
        public void Display()
        {
            Console.WriteLine("Display");
        }

        public string ToString() // compiler warning here (use new keyword, but in reality you wouldn't provide a new ToString method you would use an override)
        {
            Console.WriteLine("ToString Class");
            return "";
        }

    }

    class Program
    {
        static void Main(string[] args)
        {
            IMyInterface objClass = new ClassA();
            objClass.ShowMe();
            //objClass.Display(); // wrong, interface does not know about a Display method

            objClass.ToString();
            Console.ReadKey();
        }
    }
}


Conclusion: Your question is "bad" because your understanding of what an interface is and how inheritance works is wrong.

* you don't override interface methods - you implement them
* if you know the type of an object (or can test for a concrete type like ClassA)
you can recast your interface typed object to the concrete type and call your method:
C#
ClassA classA = objClass as ClassA;
if(classA != null)
classA.Display();

* your example code won't compile (a lot of errors) - so maybe implement a real example (like I did above) play with it, will give you a better understanding.
* What SA told you is why a ToString method is found on any Interface type, but I don't think you even thought about that.

So:
* play with an example
* read (again) about inheritance, interfaces, and override vs. new in C#
* Try to understand when to use each of these patterns
* Use interfaces the right way and you won't ask questions like this (because if you wonder "why can't I call this method" most likely your architecture is wrong.
 
Share this answer
 
Comments
Mahesh Kumar Badgujar 20-Jul-12 1:02am    
Thanks for the answer my intelligent friend.
well, there are many errors in my example because i didn't write it here to compile but to make somebody understand my question.

and What SA told me is actually what i was searching for.

ok

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