Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does an inner class method hide all the enclosing class methods with the same name?

Considering the java code below:

class Enclosing {
    void method(){}
    void method(String str){}

    class Inner {
        void method(){}
    }   
}

I am reading a book which tells me that Inner.method() will hide both versions of Enclosing.method(), which means it is an error if I call method(aString) somewhere in class Inner.

Why is the language designed like that?

Update:
According to the answer given by @Debosmit Ray, it is related to shadowing. I have read the docs and understood what it is.

What still confusing me is why method shadowing is based on method name not method signature?

like image 805
free6om Avatar asked Mar 18 '16 06:03

free6om


People also ask

Can an inner class method have access to the fields of the enclosing class?

Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class.

What are the disadvantages of inner class?

Inner classes have their disadvantages. From a maintenance point of view, inexperienced Java developers may find the inner class difficult to understand. The use of inner classes will also increase the total number of classes in your code.

Which inner class does not have a name?

An anonymous class has access to the members of its enclosing class. An anonymous class cannot access local variables in its enclosing scope that are not declared as final or effectively final.

Is Outer class Cannot be private but an inner class can be private?

Inner Class You just need to write a class within a class. Unlike a class, an inner class can be private and once you declare an inner class private, it cannot be accessed from an object outside the class. Following is the program to create an inner class and access it.


2 Answers

Non-static nested class or inner classes are used as a way to logically group classes that are only used in one place; it makes the code more readable and promotes encapsulation.

From [docs],

If a declaration of a type (such as a member variable or a parameter name) in a particular scope (such as an inner class or a method definition) has the same name as another declaration in the enclosing scope, then the declaration shadows the declaration of the enclosing scope.

Shadowing here would mean that if you have a variable x in the outer class and another variable x in the inner class, modifying x in the inner class would not affect the x in the outer class.

I quite liked this question and the point you brought up. Does my explanantion help you understand?

like image 77
Debosmit Ray Avatar answered Sep 29 '22 19:09

Debosmit Ray


Had you found the correct tag for your question, you would have understood the concept better already! Take look at the tag info for shadowing right on SO.

In computer programming, shadowing occurs when a variable declared within a certain scope (decision block, method or inner class) has the same name as a variable declared in an outer scope. This can lead to confusion, as it may be unclear which variable subsequent uses of the shadowed variable name refer to, which depends on the name resolution rules of the language.

One of the first languages to introduce variable shadowing was ALGOL, which first introduced blocks to establish scopes. It was also permitted by many of the derivative programming languages including C++ and Java.

The C# language breaks this tradition, allowing variable shadowing between an inner and an outer class, and between a method and its containing class, but not between an if-block and its containing method, or between case statements in a switch block.

Wikipedia link (doesn't provide much though)


Why is the language designed like that?

Let me give you a real world analogy to might help you understand.

Think of a building (1) that has a button named Turn on lights (3). When you press that button, it turns on all the lights in the building. Now think of a cubicle (2) inside that building. There is a small lamp in that cubicle, and a similar button named Turn on lights. Now when you press that button, what do you want it to do -- turn on all the lights of the building, or just the lamp in the cubicle? Probably the latter. Though both the buttons have the same name (4), they behave differently depending on their place (5).

Now apply this analogy to OOP. Look at the words in italics one more time, and match up!

  1. building --> Enclosing class
  2. cubicle --> Inner class
  3. Turn on lights --> Method
  4. name --> Method name/signature
  5. place --> Scope

Please note that the analogy doesn't take into consideration many other concepts of OOP, but I think it might help you understand the why part of your question.


Reply to Update:

What still confusing me is why method shadowing is based on method name not method signature?

Your question doesn't seem to be a valid one. You will understand this shortly.

You said shadowing is not based on method signature. If what you mean by this is: "if inner class method has same signature as enclosing class method, then shadowing doesn't take place", then you are wrong. Try it out by making a another method inside the inner class like void method(String str) and then calling that method inside the inner class. You will see that it is obviously shadowed.

And the reason why you got an error when you invoked method(aString) inside the inner class is completely something else-- the method method(String str) doesn't even exist inside the inner class scope.

Feel free if you need further clarification.

like image 35
Sнаđошƒаӽ Avatar answered Sep 29 '22 19:09

Sнаđошƒаӽ