Different name reusing techniques in Java


In Java, there exist different name reusing techniques for various types of entities, sucha s variables, methods, datatypes or packages. These techniques affect the accessibility and behavior of the entities according to their need and use. In this article, we will discuss four common ways to reuse a name in Java: overriding, hiding, overloading and shadowing

Name reusing techniques in Java

Shadowing

This technique allows a local variable to have the same name as another field or member of the enclosing class. In this case, the previous implementation of the member gets shadowed by the declaration of a new variable. The shadowed variable cannot be accessed by its simple name in the shadowed scope.

Example of Shadowing

The following example shows the shadowing of a variable

class OuterClass {
   int val1 = 12109659; // instance variable
   void printX() {
      int val1 = 11368; // local variable
      System.out.println("Value 1: " + val1); // belongs to current method
      System.out.println("Value 2: " + this.val1); // belongs to class
   }
}
public class Shwd {
   public static void main(String[] args) {
      OuterClass oc = new OuterClass(); // creating class object
      oc.printX(); // method call
   }
}

Output

Value 1: 11368
Value 2: 12109659

In the above code, we have shadowed the instance variable ‘val1’ in the method ‘printX()’. To access the ‘val1’ we used the ‘this’ keyword.

Hiding

This technique allows a subclass to hide a static method or a field defined in its superclass. The subclass entity must have the same name and signature as the superclass entity. Most of us mistake hiding as an overriding. In method overriding, the child class replaces the original method implementation with a new method but in hiding, we just hide the superclass method. Also, we can’t override the static method.

Example of Hiding

The following example illustrates the hiding of a method.

class ClassA {
   public static void print() {
      System.out.println("This is Super Class method");
   }
}
class ClassB extends ClassA {
   public static void print() {
      System.out.println("This is Sub Class method");
   }
}
public class Main {
   public static void main(String[] args) {
      ClassA s1 = new ClassA(); // creating object
      ClassA s2 = new ClassB();
      // calling the methods
      s1.print();
      s2.print();
      ClassB.print();
   }
}

Output

This is Super Class method
This is Super Class method
This is Sub Class method

In the above code, we have defined a ‘Super’ and a ‘Sub’ class along with a ‘print()’ method. The ‘print()’ method of ‘Sub’ class hides the method of ‘Super’ class. If ‘s2.print’ would have printed the content of subclass then this example would have been considered as method overriding instead of hiding.

Method Overloading

When we create two or more methods of the same name but with different lists of parameters in the same class body then, we call it method overloading. The compiler determines method invocation at compile time based on the argument types

Example of Method Overloading

This example shows the implementation of method overloading.

public class Ovrld {
   public static void methodA(String st1) {
      System.out.println("First method string: " + st1);
   }
   public static void methodA(String st1, String st2) { // overloading
      System.out.println("Second Method");
      System.out.println("String1: " + st1 + ", String2: " + st2);
   }
   public static void main(String args[]) {
      Ovrld obj = new Ovrld();
      // method calling
      obj.methodA("Tutorialspoint");
      obj.methodA("Simple", "Learning");
   }
}

Output

First method string: Tutorialspoint
Second Method
String1: Simple, String2: Learning

In the above code, we have defined two methods of the same name but different parameters. During calls, we used different arguments.

Method Overriding

When we create two or more methods of the same name and with the same list of parameters but in the Super and Sub-classes then, we call it method overriding. The returns types of methods are also the same.

Example of Method Overriding

The following example demonstrates the overriding of a method.

class Ovriid1 {
   public void methodA(String st1) {
      System.out.println("First method string: " + st1);
   }
}
public class Ovriid2 extends Ovriid1 {
   public void methodA(String st1) {
      System.out.println("Second Method string: " + st1);
   }
   public static void main(String args[]) {
      Ovriid1 obj1 = new Ovriid1();
      Ovriid1 obj2 = new Ovriid2();
      obj1.methodA("Tutorialspoint");
      obj2.methodA("Tutorialspoint");
   }
}

Output

First method string: Tutorialspoint
Second Method string: Tutorialspoint

In the above code, the sub-class ‘Ovriid2’ overrides the ‘methodA()’ of super-class.

Conclusion

In the article, we learned various techniques for reusing the names of variables and methods such as method overloading, hiding, shadowing and method overriding. We have also seen how to practically implement them in our Java programs.

Updated on: 02-Aug-2023

96 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements