Reuse of names leads to obscuration or shadowing, that is, the names in the current scope mask those defined elsewhere. This creates ambiguity especially when the originals need to be used and also leaves the code hard to maintain. The problem gets further aggravated when the reused name is defined in a different package.
Wiki Markup |
---|
According to the Java Language Specification \[[JLS 05|AA. Java References#JLS 05]\] section 6.3.2 Obscured Declarations: |
...
This noncompliant example implements a class that reuses the name of the class java.util.Vector
. The intent of this class is It attempts to introduce a different condition for the isEmpty
method for interfacing with native legacy code, by overriding the corresponding method in java.util.Vector
.
A future programmer may not know about this extension and may incorrectly use the Vector
idiom to use the original java.util.Vector
class, by adding an import statement. Since Because a type (Vector
class) can obscure a package name (java.util.Vector
), the custom class Vector
defined in the same package as VectorUser
, takes precedence, as a result causing . This causes undesirable effects as a direct consequence of by violating the programmer's assumptions.
Well defined import statements do resolve these issues but may get confusing when the reused name is defined in a different package. Moreover, a common (and misleading) tendency is to include the import statements after writing the code (many IDEs allow automatic inclusion as per requirements). As a result, such instances can go undetectedThis can create even more ambiguity with respect to the names.
Code Block | ||
---|---|---|
| ||
class Vector { private int val = 1; public boolean isEmpty() { if(val == 1) //compares with 1 instead of 0 return true; else return false; } //other functionality is same as java.util.Vector } import java.util.Vector; // desired functionality (line added later) public class VectorUser { public static void main(String[] args) { Vector v = new Vector(); if(v.isEmpty()) System.out.println("Vector is empty"); } } |
...
This compliant solution declares the class Vector
with a different name:.
Code Block | ||
---|---|---|
| ||
class MyVector { //other code } |
Noncompliant Code Example
This noncompliant code specimen reuses the name of the val instance field in the scope of an instance method. This behavior can be classified as shadowing.
...
Noncompliant Code Example
Strange as it may sound, method Method shadowing in different scopes becomes possible when two or more packages are used. Method shadowing is distinct from method overloading as subclasses are allowed to inherit overloadings defined in the base class. It differs from hiding in that the methods do not have to be declared static
. It is also distinct from method overriding as exemplified in the following code segment.
...
Note that class y.C
is accessible from the package x
and so is its doLogic()
method. If however, the main()
method defined in class A
tries to polymorphically invoke y.doLogic()
as shown, the override corresponding to class B in package x
will take precedence. This is because the doLogic()
methods in classes x.A
and x.B
are not visible from class y.C
due to the default
access specifier. As a result, the class x.C
is not considered a part of the overriding hierarchy. Notably, the code behaves as expected if the access specifiers of all the methods are changed to public
.
...
It is highly recommended that a different name be used so that it is clear that the class residing in another package is not meant to be a part of the overriding chain. A programmer can proceed to invoke methods on it by explicitly using the class name. Even when all the classes define methods with a public
access specifier, it is better to avoid reusing names since because an evolving class could can limit method accessibility anytime in the future causing unexpected results.
Code Block | ||
---|---|---|
| ||
package x; public class A { void doLogic() { /* print 'A' */ } public static void main(String[] args) { y.C.doSequence(); // explicitly invokes doSequence() of class y.C and prints 'C' y.C.doSequence(); } } package x; public class B { /* ... */ } package y; // different package public class C extends x.B { public void doSequence() { /* print 'C' */ } // now renamed } |
...