开发者

How can I restrict method calling in the same class?

Recently I attended an interview, the interviewer is asking a question about restricting of method access.

Q: In one class I have 3 methods (i.e. method1(), method2(), method3()), I am calling those 3 methods in same class, but I should call only first 2 method, I should restrict 3rd method, so nobody should call method3(). How can I do it ?

开发者_开发知识库

The interviewer gave an hint also (i.e. by using object or reference) . And at the end of interview he said answer is reference.

I was confused with his question, any body having an idea of what he is pointing to ?

Thanks, Ranjith


Maybe he meant that method1 and method 2 should be static (i. e. not needing a reference to an existing instance), and method3 should be an instance method?

In this case, the first two methods could be called from within Main without instantiating an object reference.


Well, your question isn't very clear... but if method3() should only be callable from within the class that created it, you can just make it private.

If that isn't what you're asking, please clarify the question.


I wonder if the interview was trying to elicit your understanding of public, protected and private access level modifiers in the Java language?

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜