Different name reusing techniques in Java

Overriding An instance method overrides all accessible instance methods with the same signature in superclasses, enabling dynamic dispatch; in other words, the VM chooses which overriding to invoke based on an instance’s run-time type. Overriding is fundamental to object-oriented programming and is the only form of name reuse that is not generally discouraged:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Base Class
class Base {
public void f() 
{ }
}
  
// Derived Class
class Derived extends Base {
  
// overrrides Base.f()
public void f() 
{ } 
}

chevron_right


Hiding A field, static method, or member type hides all accessible fields, static methods, or member types, respectively, with the same name (or, for methods, signature) in supertypes. Hiding a member prevents it from being inherited :

filter_none

edit
close

play_arrow

link
brightness_4
code

// Base class
class Base {
   public static void f() 
   { }
}
  
// Derived class
class Derived extends Base {
  
   // hides Base.f()
   public static void f()
   { }
}

chevron_right


Overloading Methods in a class overload one another if they have the same name and different signatures. The overloaded method designated by an invocation is selected at compile time:



filter_none

edit
close

play_arrow

link
brightness_4
code

// Base class
class GeeksForGeeks {
  
   // int overloading
   public void f(int i) 
   { } 
   
   // String overloading
   public void f(String s) 
   { } 
}

chevron_right


Shadowing A variable, method, or type shadows all variables, methods, or types, respectively, with the same name in a textually enclosing scope. If an entity is shadowed, you cannot refer to it by its simple name; depending on the entity, you cannot refer to it at all:

filter_none

edit
close

play_arrow

link
brightness_4
code

// Base class
class GeeksForGeeks {
  
static String sentence = "I don’t know.";
  
  public static void main(String[] args) {
  
     // shadows static field
     String sentence = "I know!";
  
     // prints local variable
     System.out.println(sentence); 
  }
}

chevron_right


Although shadowing is generally discouraged, one common idiom does involve shadowing. Constructors often reuse a field name from their class as a parameter name to pass the value of the named field. This idiom is not without risk, but most Java programmers have decided that the stylistic benefits outweigh the risks:

filter_none

edit
close

play_arrow

link
brightness_4
code

class Shirt {
   private final int size;
  
   // Parameter shadows Shirt.size
   public Shirt(int size) { 
     this.size = size;
   }
}

chevron_right


Obscuring A variable obscures a type with the same name if both are in scope: If the name is used where variables and types are permitted, it refers to the variable. Similarly, a variable or a type can obscure a package. Obscuring is the only kind of name reuse where the two names are in different namespaces: variables, packages, methods, or types. If a type or a package is obscured, you cannot refer to it by its simple name except in a context where the syntax allows only a name from its namespace. Adhering to the naming conventions largely eliminates obscuring

filter_none

edit
close

play_arrow

link
brightness_4
code

public class Obscure {
  
  // Obscures type java.lang.System
  static String System;
  
  public static void main(String[] args) {
  
     // Next line won’t compile: 
     // System refers to static field
     System.out.println("hello, obscure world!");
  }
}

chevron_right




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.