开发者

Doubt with the implementation of interfaces

开发者 https://www.devze.com 2023-01-20 15:22 出处:网络
Firstly, this is just an Object Oriented Programming question and does not apply to any Language in particular.

Firstly, this is just an Object Oriented Programming question and does not apply to any Language in particular.

This is quite embarassing for me. This incident happened @ work and I was too shy to clarify this with my colleagues as it would indicate a poor understanding of Object Oriented Programming on my part. So here is the incident :

There is a class A which implements interface I. This interface has a method M. Class A has defined the body or rather implementation of this method. Now I have a tool with which I can find out which other class or program uses, or in other words calls Class A -> method M.When I used this tool, it did not return any results; An indication which I took as no class is calling method M.

When I shared my observations with a senior colleague, he simply went to the interface I -> method M, and used the tool on this. It returned quite a few results. He told me that these are the开发者_运维知识库 classes or methods which call method M and asked me to proceed.

This result is obviously a list of those classes which implement interface I. But what I didn't understand is that how is this the set of classes or methods which call Class A -> method M. Since I had confidently told my colleague that no class calls method M, after he showed me the result list, I was too embarassed to ask him how is that the result that I am looking for.

Any idea what my colleague is hinting at?

Regards, Mithun


The whole idea behind having interfaces for the classes is so that the interfaces will be used to pass the instances. This is to provide loose coupling. Now, since you do not actually pass the class around but the interface specific to that class, eg :

void someMethod (ISomeClass intobject)

instead of

void someMethod (SomeClass obj)

so when you are trying to call a method within ISomeClass / SomeClass called myMethod in someMethod, it is actually

 intobject.myMethod()

Hence, all usages of myMethod refer back to ISomeClass and not really to SomeClass

Edit: Maybe its just me, but I always believe that when you dont understand something, it is better to open your mouth to ask a question and be mistaken for a fool than to keep it shut and prove yourself to be one!


I think you should ask your guy what is going on. You shouldn't be afraid to admit you don't know; there is nothing wrong with not knowing, if you learn from it. I am wrong all the time and am not in the least embarrassed by it, if I can get to the next step and learn something.

I think what you are seeing is that nothing is using Class A -> M, but that there are other implementations of I which use M. The tool found where M is being used by other implementations.


When I used this tool, it did not return any results; An indication which I took as no class is calling method M.

Does no results mean that?

But what I didn't understand is that how is this the set of classes or methods which call Class A -> method M.

Is that really what the results mean?

How does this tool work? What do the results mean? Not sure if it's OOP you don't understand, or just this magic tool?

0

精彩评论

暂无评论...
验证码 换一张
取 消