Vbp
Vbp

Reputation: 1982

Memory allocation Java

If we have Class A which has an integer i in it and Class B which is inheriting Class A with an integer i too. At run time how many slots of memory will be allocated for i ? This question was asked in my exam few days back and this is all I could remember. Thanks!

Upvotes: 3

Views: 132

Answers (1)

Bohemian
Bohemian

Reputation: 425418

Both instance variables will have memory allocated. They are separate instance variables, unrelated to each other other than that they share the same name.

Not directly related to the questions, but if the superclass's variable is not private, the situation you are describing is called shadowing - where the subclass's variable "hides" the superclases's variable if just referenced by the variable name alone. You would have to use super.i to reference it.

Upvotes: 3

Related Questions