Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

What qualifies as shadowing?

 
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,

From one of Enthuware's mock exams:

What will be the result of attempting to compile and run class B?

The answer being It will print 20 and then 10.

The explanation, however, states the following:

Note that a final variable can be shadowed. Here, although fi in A is final, it is shadowed by fi of B. So b.fi = 20; is valid since B's fi is not final.



It was my understanding that shadowing happens to variables within a class (i.e. a local/parameter variable can shadow a class/instance variable). In this case, however, a subclass variable seems to 'shadow' a superclass variable. Do both of the situations I described qualify as shadowing, or am I mixing things up?

Thanks again!

Regards,
Shane
 
Enthuware Software Support
Posts: 4450
41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is a case of hiding and not shadowing.
JLS section 8.5 says:


If a class declares a member type with a certain name, then the declaration of that type is said to hide any and all accessible declarations of member types with the same name in superclasses and superinterfaces of the class.


Explanation should be corrected.
 
Rebecca Wolf
Ranch Hand
Posts: 113
7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Anilprem wrote:It is a case of hiding and not shadowing.
JLS section 8.5 says:


If a class declares a member type with a certain name, then the declaration of that type is said to hide any and all accessible declarations of member types with the same name in superclasses and superinterfaces of the class.


Explanation should be corrected.


Hi Paul,

Thanks for the clarification.

According to the JLS explanation, could one say that overriding (a method) is a form of hiding?
 
Ranch Hand
Posts: 231
12
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JLS quote above is talking about member types (for example, nested classes, nested interfaces).

A member in a subclass can hide the same member (with the same name) that would otherwise be inherited from a superclass. This applies to all members (fields, static methods, nested classes etc) except non-static methods.

A non-static method in a subclass can override a non-static method (with the same name) that would otherwise be inherited from a superclass.

A member declared in a class can shadow the same member (with the same name) declared in a local scope within the same class.

A member declared in a class can obscure a different member (with the same name) declared in the same class.
 
Paul Anilprem
Enthuware Software Support
Posts: 4450
41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel is right. Section 8.3 is pertinent in this regard:


If the class declares a field with a certain name, then the declaration of that field is said to hide any and all accessible declarations of fields with the same name in superclasses, and superinterfaces of the class.

In this respect, hiding of fields differs from hiding of methods (§8.4.8.3), for there is no distinction drawn between static and non-static fields in field hiding whereas a distinction is drawn between static and non-static methods in method hiding.





If a class C declares or inherits a static method m, then m is said to hide any method m', where the signature of m is a subsignature (§8.4.2) of the signature of m', in the superclasses and superinterfaces of C that would otherwise be accessible to code in C.

It is a compile-time error if a static method hides an instance method.

In this respect, hiding of methods differs from hiding of fields (§8.3), for it is permissible for a static variable to hide an instance variable. Hiding is also distinct from shadowing (§6.4.1) and obscuring (§6.4.2).



Hiding of instance methods does not happen in the same sense as the term hiding is used for fields and static methods because the rules of "overriding" apply in this case. An instance method can only override an instance method of a super class (except a final method, of course) and can never hide a superclass's instance method (not even a final method). It is not allowed for an instance method to override a final method of the superclass. Also, it is a compile-time error if an instance method overrides a static method.

 
Daniel Cox
Ranch Hand
Posts: 231
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to clarify my post.

Daniel Cox wrote:A member declared in a class can shadow the same member (with the same name) declared in a local scope within the same class.


By "same member", I mean that a field can shadow another field. A field cannot shadow a class.

Daniel Cox wrote:A member declared in a class can obscure a different member (with the same name) declared in the same class.


By "different member", I mean that a field can obscure a class. A field cannot obscure another field.

The code below shows a field that obscures a class. Line 3 fails to compile because System refers to the static field and not the System class.

 
On my planet I'm considered quite beautiful. Thanks to the poetry in this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic