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?
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++.
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.
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 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.
There are actually two distinct public instance variables called a
.
Foo.a
variable.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With