Page 1 of 1

About Question enthuware.ocajp.i.v8.2.1147 :

Posted: Thu Jan 12, 2017 2:49 pm
by babur12
From this quote "Constructors and static initializers are not members and therefore are not inherited."

Since static initializers are not inherited and there is nothing said about instance initializers, does that mean instance initializers?

Re: About Question enthuware.ocajp.i.v8.2.1147 :

Posted: Thu Jan 12, 2017 11:30 pm
by admin
Instance initializers are not inherited either.

Re: About Question enthuware.ocajp.i.v8.2.1147 :

Posted: Mon Dec 04, 2017 8:40 am
by Rinkesh
Sir can you give me a example of static initializer?

Re: About Question enthuware.ocajp.i.v8.2.1147 :

Posted: Mon Dec 04, 2017 9:32 am
by admin

Re: About Question enthuware.ocajp.i.v8.2.1147 :

Posted: Wed Jan 03, 2018 9:53 am
by Hiar85
"Note that 'i' is not public or protected. So it will be inherited only if both the classes are in same package."
Imho this is not the reason that this answer is correct.
At runtime, when an actual object is created, the child object contains all the members of its parent class, even the private ones.
See:
https://stackoverflow.com/questions/471 ... ate-fields

Re: About Question enthuware.ocajp.i.v8.2.1147 :

Posted: Wed Jan 03, 2018 10:29 pm
by admin
Inheritance is a technical term and has a specific meaning in Java language as defined by the Java Language Specification. private members are not inherited. A subclass cannot access the private members of the base class. Plain and simple. When you talk about the word "inheritance" in context of Java, this is the meaning you should refer to.

Now, from a memory perspective, obviously, when the JVM allocates space for an object of a subclass, that space is the sum of all the space required for members of the base class(es) and the space required for the members of the subclass. So yes, superclass fields are there in the subclass in some form but that is totally irrelevant from Java programming (and the certifcation exam) point of view.

Re: About Question enthuware.ocajp.i.v8.2.1147 :

Posted: Tue Mar 06, 2018 10:00 am
by Rick84
Objects of class B will always have a member variable named i.
Note that 'i' is not public or protected. So it will be inherited only if both the classes are in same package.
I think this question is confusing. As the explanation states, it won't always have a member variable i, but only if the classes are in the same package. I feel this question might be clearer if the member variables are defined public or protected.

Re: About Question enthuware.ocajp.i.v8.2.1147 :

Posted: Tue Mar 06, 2018 11:05 am
by admin
As per the code that is given in the question, B will always have i. The explanation is merely reminding that if class B belongs to a different package, then it will not inherit i.