SCJP 1.4<br />SCWCD 1.4
when You create an instance, You just create it, not superclass instances, and when You call a method defined in a superclass, your instance executes the code from superclass definition, but in its own context
SCJP 1.4<br />SCWCD 1.4
"(When an object is created, instances of all its super-classes are also created)." Really?
Whenever a new class instance is created, memory space is allocated for it with room for all the instance variables declared in the class type and all the instance variables declared in each superclass of the class type, including all the instance variables that may be hidden (�8.3). If there is not sufficient space available to allocate memory for the object, then creation of the class instance completes abruptly with an OutOfMemoryError. Otherwise, all the instance variables in the new object, including those declared in superclasses, are initialized to their default values (�4.5.5).
Memory for a new instance of that class is allocated from the garbage-collected heap, and the instance variables of the new object are initialized to their default initial values
In the above program, class Q's instance variable b hide class P's b and class Q inherits m() method from class P, how to explain that when object q calls m() method, it doesn't "see" its own b but class P's b?
Originally posted by Claire Yang:
In the above program, class Q's instance variable b hide class P's b and class Q inherits m() method from class P, how to explain that when object q calls m() method, it doesn't "see" its own b but class P's b?
.Methods access variables only in context of the class of the object they belong to
suggest the instance that actually executes m(), is an Instance of P (The superclass) but if it were an instance of P it would call P implementation of b() instead of Q implementation...(When an object is created, instances of all its super-classes are also created).
SCJP 1.4<br />SCWCD 1.4
SCJP 1.4<br />SCWCD 1.4
Consider Paul's rocket mass heater. |