If I have a class A which is super class of classes B and C and I have
A as[] = new A[1];
then how much space will as occupy the size of A, B or C?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The object
asis an array of length 1. In Java, arrays containing objects actually contain references to objects, and a reference takes either 4 or 8 bytes depending on the hardware architecture.You haven’t allocated any objects of type
A, or any of its subtypes, so the total memory used is the memory occupied by a single reference (4 or 8 bytes).If you were to do
then you would additionally have however much memory
Atakes up, which depends on whatAcontains. If you didthe memory usage would be whatever
Btakes up, which depends onB‘s members but would be ≥A‘s memory requirements.