Home  >  Article  >  Java  >  Different name reuse techniques in Java

Different name reuse techniques in Java

PHPz
PHPzforward
2023-08-28 14:49:061374browse

Different name reuse techniques in Java

In Java, there are different name reuse techniques for various types of entities such as variables, methods, data types, or packages. These technologies influence the accessibility and behavior of entities based on their needs and usage. In this article, we will discuss four common ways to reuse names in Java: overriding, hiding, overloading, and masking

Name reuse technology in Java

shadow

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 is obscured by the declaration of the new variable. Hidden variables cannot be accessed by their simple names in hidden scopes.

Shadow Example

The following example shows the shading of variables

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 hide the instance variable "val1" in the method "printX()". To access "val1" we use the "this" keyword.

hide

This technique allows a subclass to hide static methods or fields defined in its superclass. Subclass entities must have the same name and signature as the superclass entity. most Some of us mistakenly believe that hiding is overwhelming. In method overriding, subclasses replace Implement the original method with the new method, but in hiding, we just hide Super class method. Also, we cannot override static methods.

Hide example

The following example illustrates method hiding.

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 define a "Super" and a "Sub" class and a "print()" method. The ‘print()’ method of ‘Sub’ class hides the method of ‘Super’ class. if 's2.print' will print the content of the subclass, then this example will be considered Overridden as a method rather than hidden.

Method overloading

When we create two or more methods with the same name but different lists Then, if the parameters are in the same class body, we call it method overloading. translater Determine compile-time method calls based on parameter types

Method overloading example

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 define two methods with the same name but different parameters. During the call we used different arguments.

Method overriding

When we create two or more methods with the same name and the same parameter list in the super class and subclass, we call it method overriding. The return type of the method is also the same.

Method rewriting example

The following example demonstrates method overriding.

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 subclass "Ovriid2" overrides the superclass's "methodA()".

in conclusion

In this article, we learned about various techniques for reusing variable and method names, such as method overloading, hiding, hiding, and method overriding. We also saw how to actually implement them in a Java program.

The above is the detailed content of Different name reuse techniques in Java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete