Introduction
Java, a versatile and widely-used programming language, offers a variety of features that empower developers to write efficient and maintainable code. Two important concepts in Java, variable hiding and method hiding, often come into play when working with classes and inheritance. In this comprehensive blog post, we’ll explore these two concepts, provide clear examples, and discuss the differences between them. Along the way, we’ll also touch on essential Java programming concepts, such as ‘truncation in Java,’ and ‘method hiding in Java,’ to illustrate their relevance within the context of variable and method hiding.
Variable Hiding in Java
Variable hiding is a concept in Java that occurs when a subclass declares a variable with the same name as a variable in its superclass. When this happens, the subclass’s variable “hides” the superclass’s variable with the same name. Variable hiding can be achieved using the same variable name as the superclass variable in the subclass. Let’s take a look at an example to understand variable hiding better:
“`java
class Parent {
int x = 10;
}
class Child extends Parent {
int x = 20;
void display() {
System.out.println(“Child class variable x: ” + x);
System.out.println(“Parent class variable x: ” + super.x);
}
}
public class Main {
public static void main(String[] args) {
Child child = new Child();
child.display();
}
}
“`
In this code, we have a `Parent` class with an instance variable `x` initialized to 10. The `Child` class extends `Parent` and has its own instance variable `x` initialized to 20. In the `display` method of the `Child` class, we print the values of both `x` variables.
When we create an instance of the `Child` class and call the `display` method, we get the following output:
“`
Child class variable x: 20
Parent class variable x: 10
“`
This demonstrates variable hiding. The `Child` class’s variable `x` hides the `Parent` class’s variable `x`. To access the `Parent` class’s `x`, we use the `super` keyword.
Method Hiding in Java
Method hiding is another concept in Java, but it deals with methods rather than variables. Method hiding occurs when a subclass defines a static method with the same signature as a static method in its superclass. In this case, the subclass’s static method “hides” the superclass’s static method. To understand method hiding better, let’s consider an example:
“`java
class Parent {
static void display() {
System.out.println(“Static method in the Parent class.”);
}
}
class Child extends Parent {
static void display() {
System.out.println(“Static method in the Child class.”);
}
}
public class Main {
public static void main(String[] args) {
Parent parent = new Parent();
Parent childAsParent = new Child();
Child child = new Child();
parent.display();
childAsParent.display();
child.display();
}
}
“`
In this code, we have a `Parent` class with a static method `display`, and a `Child` class that extends `Parent` and also has a static method `display`. We create instances of both classes and call the `display` method on each.
The output of this code will be:
“`
Static method in the Parent class.
Static method in the Parent class.
Static method in the Child class.
“`
As we can see, the static method in the `Child` class hides the static method in the `Parent` class. This is because static methods are associated with the class itself, not with instances of the class.
Key Differences Between Variable Hiding and Method Hiding
Now that we’ve explored both variable hiding and method hiding in Java, let’s highlight the key differences between these two concepts:
1. Type of Members:
– Variable Hiding: Variable hiding deals with instance variables. Subclasses can hide superclass instance variables by declaring variables with the same name.
– Method Hiding: Method hiding involves static methods. Subclasses can hide superclass static methods by defining static methods with the same name and signature.
2. Access Mechanism:
– Variable Hiding: To access the hidden superclass variable, you can use the `super` keyword.
– Method Hiding: The access to hidden superclass static methods is determined by the reference type. If the reference type is the superclass, it calls the superclass’s method. If the reference type is the subclass, it calls the subclass’s method.
3. Inheritance:
– Variable Hiding: Variable hiding doesn’t affect the inheritance hierarchy. Subclasses can have variables with the same names as superclass variables without changing the inheritance structure.
– Method Hiding: Method hiding can alter the inheritance hierarchy slightly, as the static method in the subclass takes precedence over the static method in the superclass.
4. Static vs. Instance:
– Variable Hiding: This concept applies to instance variables, which are associated with object instances.
– Method Hiding: Method hiding applies to static methods, which are associated with classes.
Relevance of Method Hiding in Java
Method hiding in Java can be a powerful tool in scenarios where you need to provide specialized behavior in a subclass while maintaining the structure and signatures of methods defined in the superclass. Here are some situations where method hiding can be beneficial:
1. Overriding Static Methods: In some cases, you might have a static method in a superclass that you want to “override” with a different implementation in a subclass. Method hiding allows you to do this without breaking the method signature.
2. Utility Classes: Method hiding is commonly used in utility classes, where you have a collection of static methods. Subclasses can provide their own versions of these methods to suit their specific needs.
3. API Design: When designing an API, method hiding can be used to provide default implementations for static methods while allowing users of the API to override these methods in their own classes.
Truncation in Java
Before we conclude our discussion on variable and method hiding in Java, let’s briefly touch on another concept, ‘truncation in Java.’ Truncation refers to the loss of precision that can occur when converting data from one type to another. This often happens when you convert a larger data type to a smaller one, and some information is lost in the process. Here’s a simple example in Java:
“`java
double largeValue = 1234.56789;
int truncatedValue = (int) largeValue;
System.out.println(“Truncated Value: ” + truncatedValue);
“`
In this code, we have a `double` value, `largeValue`, which is converted to an `int` using casting. The result is a truncated value, and the output will be:
“`
Truncated Value: 1234
“`
In this case, the decimal part of the `double` value is truncated when converting it to an `int`.
Conclusion
In Java, both variable hiding and method hiding are important concepts to understand, especially when working with class hierarchies and inheritance. Variable hiding allows subclasses to declare instance variables with the same name as those in the superclass, providing local scope to the subclass.
Method hiding, on the other hand, enables subclasses to define static methods with the same name as those in the superclass, providing specialized behavior without altering the method signature.