Different name reusing techniques in Java
Last Updated :
05 Aug, 2021
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:
Java
// Base Class
class Base {
public void f()
{ }
}
// Derived Class
class Derived extends Base {
// overrides Base.f()
public void f()
{ }
}
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 :
Java
// Base class
class Base {
public static void f()
{ }
}
// Derived class
class Derived extends Base {
// hides Base.f()
public static void f()
{ }
}
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:
Java
// Base class
class GeeksForGeeks {
// int overloading
public void f(int i)
{ }
// String overloading
public void f(String s)
{ }
}
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:
Java
// 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);
}
}
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:
Java
class Shirt {
private final int size;
// Parameter shadows Shirt.size
public Shirt(int size) {
this.size = size;
}
}
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
Java
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!");
}
}
Similar Reads
Different Ways of Method Overloading in Java In Java, Method overloading refers to the ability to define multiple methods with the same name but with different parameter lists in the same class. It improves code readability and reusability, Instead of assigning different names to methods performing similar operations, we can use the same name,
5 min read
run() Method in Java Thread The run() method is available in the thread class constructed using a separate Runnable object. Otherwise, this method does nothing and returns. We can call the run() method multiple times. The run() method can be called in two ways which are as follows:Using the start() method.Using the run() metho
3 min read
Naming Conventions in Java A programmer is always said to write clean codes, where naming has to be appropriate so that for any other programmer it acts as an easy way out to read the code. At a smaller level, this seems meaningless but think of the industrial level where it becomes necessary to write clean codes in order to
5 min read
Using Static Variables in Java Here we will discuss the static variables in java. Java actually doesnât have the concept of Global variable. To define a Global variable in java, the keyword static is used. The advantage of the static variable is discussed below. Now geeks you must be wondering out what are the advantages of stati
3 min read
Standard Practices for Protecting Sensitive Data in Java The Java platform provides an environment for executing code with different permission levels and a robust basis for secure systems through features such as memory safety. The Java language and virtual machine make application development highly resistant to common programming mistakes, stack smashi
4 min read
Demystifying Memory Management in Modern Java Versions Memory management is the backbone of Java programming, determining how efficiently your applications use system resources. In this comprehensive guide, we will explore the intricacies of memory management in modern Java versions, including Java 8, 11, and beyond. By the end of this article, you'll h
6 min read
Accessing Protected Members in Java In Java, there are four types of access modifiers. These are public, private, default, and protected. To get the idea of these modifiers, you can refer to access modifiers in java. In this article, we discuss the accessibility of protected members in different cases. Now let us discuss various scena
6 min read
Difference Between Singleton and Factory Design Pattern in Java Design patterns are essential tools for creating maintainable and scalable software. Two commonly used design patterns in Java are the Singleton and Factory patterns. This article provides an introduction to the Singleton and Factory design patterns with examples. Singleton Design Pattern in JavaThe
3 min read
Open Closed Design Principle in Java Open Closed Design Principle is one of the most important design principles in the world of software development. It's part of a group of 5 design principles commonly known by the acronym "SOLID" S - Single Responsibility Principle O - Open Closed Principle L - Liskov Substitution Principle I - Inte
12 min read
How to Optimize String Creation in Java? In Java, strings are frequently used, and optimizing their creation can help improve performance, especially when creating many strings in a program. In this article, we will explore simple ways to optimize string creation in Java.Example:Using the new keyword creates a new string object. This is st
2 min read