3

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!

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.