(Hiding the field) When does it make sense to use both the subclass field and its hidden superclass field?

In Java (as in most OO languages) you can have two classes, one of which extends the other. In both classes, you can have instances with the same name, where the subclass instance field hides the instance field of the superclass. The following is an example.

class A{
    int i;
}

class B extends A{
    int i;
}

This means that when an object is created, it has both a B-instance field and its instance field, i. You would think that you would never want this, and when you conceptually create a new "i" in class B, it means "the same one that has to do with this object." When is it not? Give an example of two classes in which we want to save both instance variables and change them.

+4
source share

All Articles