Home > Java > javaTutorial > Can Java Subclasses Override Member Variables, or Do They Just Hide Them?

Can Java Subclasses Override Member Variables, or Do They Just Hide Them?

Patricia Arquette
Release: 2024-12-19 22:15:09
Original
564 people have browsed it

Can Java Subclasses Override Member Variables, or Do They Just Hide Them?

Variable Hiding in Java

While exploring member function overriding in Java, a curiosity arose about the potential to override member variables. To investigate, consider the following code:

public class A {
    public int intVal = 1;
    public void identifyClass() {
        System.out.println("I am class A");
    }
}

public class B extends A {
    public int intVal = 2;
    public void identifyClass() {
        System.out.println("I am class B");
    }
}

public class MainClass {
    public static void main(String[] args) {
        A a = new A();
        B b = new B();
        A aRef;
        aRef = a;
        System.out.println(aRef.intVal);  // Prints 1
        aRef.identifyClass(); // Prints "I am class A"
        aRef = b;
        System.out.println(aRef.intVal);  // Prints 1
        aRef.identifyClass(); // Prints "I am class B"
    }
}
Copy after login

The output is:

1
I am class A
1
I am class B
Copy after login

The code initializes two classes, A and B, both with a member variable named intVal. B extends A and redeclares the intVal member variable. When the aRef reference variable is set to an instance of A, the intVal member variable is correctly accessed. However, when aRef is set to an instance of B, the intVal member variable still prints the value stored in the A class.

Why is this occurring?

Member Variable Hiding

In this situation, the member variable intVal in the B class is not overriding the intVal member variable in the A class. Instead, the variable is hidden. When a variable is hidden, the subclass contains both the original property from the superclass and a new property with the same name.

To access the original member variable from the superclass, you can use super.var or ((SuperClass)this).var syntax. For example:

// Access the original intVal member variable from the A class
System.out.println(aRef.intVal);

// Use super to access the original intVal member variable
System.out.println(aRef.super.intVal);
Copy after login

It's important to note that hidden variables do not need to be of the same type. They simply share the same name within the subclass. This allows you to create different implementations of the same property for different subclasses, providing flexibility and code reusability.

The above is the detailed content of Can Java Subclasses Override Member Variables, or Do They Just Hide Them?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template