Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 993
Vbp Avatar asked Dec 30 '25 17:12

Vbp


1 Answers

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.

like image 88
Bohemian Avatar answered Jan 01 '26 07:01

Bohemian



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!