4

In Java (as with most OO languages), you can have two classes, with one extending the other. You can have instance fields with the same name in both classes, where the subclass' instance field hides the superclass' instance field. An example is written below.

class A{
    int i;
}

class B extends A{
    int i;
}

This means that when the object is created, it has both its B instance field, i, and its A instance field, i. One might think that you would never want this, and that when you conceptually create a new "i" in class B, it means "the one and only i that is relevant to this object". When is this not the case? Give an example of two classes where we want to keep both instance variables and modify them.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
user442920
  • 857
  • 4
  • 21
  • 49
  • It can't have both since they are both called `i`. If one were `i` and the other were `j`, then yes. Now it is overriding it. – RaminS Apr 29 '16 at 18:50
  • @Gendarme This is known as [hiding](http://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#d5e12172) and perfectly valid. You can't override fields. – Sotirios Delimanolis Apr 29 '16 at 18:51
  • @SotiriosDelimanolis I didn't say it is invalid. What is the difference between overriding and hiding, then? They seem to do the same thing - just replace the member of the superclass. Are we arguing over semantics? – RaminS Apr 29 '16 at 18:52
  • @Gendarme simply not true - it can have both. Overriding is irrelevant. – ThisClark Apr 29 '16 at 18:53
  • 2
    @Gendarme You said _it can't have both_. That is incorrect. The code above will compile just fine. Overriding implies that the runtime will resolve the member based on the runtime type of the receiver expression. This is not true for fields. Fields are not polymorphic. – Sotirios Delimanolis Apr 29 '16 at 18:54
  • @SotiriosDelimanolis *"It can't have both"* means that the second one replaces the first one, not that it will not compile. – RaminS Apr 29 '16 at 18:56
  • @Gendarme That is also incorrect. There is no replacement. An instance of type `B` will have both `x` fields. – Sotirios Delimanolis Apr 29 '16 at 18:56
  • @SotiriosDelimanolis How do you access it, then? I don't see why it wouldn't be the same as with methods. – RaminS Apr 29 '16 at 18:57
  • @Gendarme You can access the superclass member with `super`. See [this](http://ideone.com/KG8Rgo) example. – Sotirios Delimanolis Apr 29 '16 at 18:59
  • @SotiriosDelimanolis And `super.methodName()` does not work? – RaminS Apr 29 '16 at 19:00
  • 1
    @Gendarme That's not the point. See [here](http://ideone.com/KG8Rgo). Methods are polymorphic. At runtime, they are resolved on the runtime type of the expression. Fields are resolved at compile time on the static type of the expression. They are not polymorphic. Overriding does not apply to them. The correct terminology is hiding and it is a different concept altogether. Declare `a` as a `B` variable and you'll see different behavior. – Sotirios Delimanolis Apr 29 '16 at 19:02
  • @SotiriosDelimanolis Are other languages like this too, or just Java? This is insanely disgusting. – RaminS Apr 29 '16 at 19:15
  • 1
    @Gendarme Variations of it. For example, C# will force you to use the `new` keyword for such member declarations to avoid unintended fields with the same name. See an [example](http://ideone.com/9cPSqi) (try to remove the `new` in the field declaration). – Sotirios Delimanolis Apr 29 '16 at 19:19
  • I'm voting to close this question as off-topic because it's a dump of a homework assignment that shows ZERO effort from the OP. – S.L. Barth is on codidact.com May 03 '16 at 06:46

0 Answers0