Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens when base and derived classes each have variables with the same name

Tags:

java

Consider the int a variables in these classes:

class Foo {
    public int a = 3;
    public void addFive() { a += 5; System.out.print("f "); }
}
class Bar extends Foo {
    public int a = 8;
    public void addFive() { this.a += 5; System.out.print("b " ); }
}
public class test {
    public static void main(String [] args){
        Foo f = new Bar();
        f.addFive();
        System.out.println(f.a);
    }
}

I understand that the method addFive() have been overridden in the child class, and in class test when the base class reference referring to child class is used to call the overridden method, the child class version of addFive is called.

But what about the public instance variable a? What happens when both base class and derived class have the same variable?

The output of the above program is

b 3 

How does this happen?

like image 495
moonsun Avatar asked Apr 12 '11 04:04

moonsun


People also ask

When base class and derived class have same function name What happens when derived class object calls the function?

The derived classes inherit features of the base class. Suppose, the same function is defined in both the derived class and the based class. Now if we call this function using the object of the derived class, the function of the derived class is executed. This is known as function overriding in C++.

Can derived class have same name as that of base class?

When you derive classes, ambiguities can result if base and derived classes have members with the same names. Access to a base class member is ambiguous if you use a name or qualified name that does not refer to a unique function or object.

What happens if the base and derived class contains definition of a function with same prototype?

38. What happens if the base and derived class contains definition of a function with same prototype? A. Compiler reports an error on compilation.

When we use the same function name in both the base and derived classes the function in base class is declared as?

When the same function name is used in both base and derived classes, then the function in base class is declared as virtual using the keyword virtual before the declaration of the function.


1 Answers

There are actually two distinct public instance variables called a.

  • A Foo object has a Foo.a variable.
  • A Bar object has both Foo.a and Bar.a variables.

When you run this:

    Foo f = new Bar();
    f.addFive();
    System.out.println(f.a);

the addFive method is updating the Bar.a variable, and then reading the Foo.a variable. To read the Bar.a variable, you would need to do this:

    System.out.println(((Bar) f).a);

The technical term for what is happening here is "hiding". Refer to the JLS section 8.3, and section 8.3.3.2 for an example.

Note that hiding also applies to static methods with the same signature.

However instance methods with the same signature are "overridden" not "hidden", and you cannot access the version of a method that is overridden from the outside. (Within the class that overrides a method, the overridden method can be called using super. However, that's the only situation where this is allowed. The reason that accessing overridden methods is generally forbidden is that it would break data abstraction.)


The recommended way to avoid the confusion of (accidental) hiding is to declare your instance variables as private and access them via getter and setter methods. There are lots of other good reasons for using getters and setters too.


It should also be noted that: 1) Exposing public variables (like a) is generally a bad idea, because it leads to weak abstraction, unwanted coupling, and other problems. 2) Intentionally declaring a 2nd public a variable in the child class is a truly awful idea.

like image 152
Stephen C Avatar answered Oct 14 '22 07:10

Stephen C