java - Inheritance and Object Creation -


base class:

public class inheritance {     int i;     inheritance() {         system.out.println("i in base class" + i);     } } 

derived class:

public class testinheritance extends inheritance {      testinheritance() {         system.out.println("i in derived class");     }      public static void main(string[] args) {         testinheritance obj = new testinheritance();             } } 

this have in mind regarding going on above.

when create object of derived class default super() called , constructor of base class called , initializes variable i.

now, question is: constructor in case initialize variable i , doesn't create concrete object of class?

from have read far there 1 object created - of derived class has i variable in it.

but time constructor of base class called , point in time constructor of derived class called how/where i stored in memory?

and case in base class abstract one.

i appreciate if can know happens in memory @ different points of time.

if have said fundamentally incorrect please let me know. want know how thing works.

physically created single object, conceptually created 2. it's when, say, baby girl born: physically one, conceptually new female has been born, new human being has been born, new sentient being has been born, new inhabitant of planet earth has been born, etc. yes, exactly. there relationship of inheritance (subtyping) here too, , did on purpose. avoid confusion, it's better there 1 object several facets; 1 object member of different (super-)classes @ same time.

now, said enough logical part, let's examine physical part. single (physical) object created following structure in memory

+--------------------------+----------------------------+ |             b            |              c'            + +--------------------------+----------------------------+ 

the first part (b) contains fields inherited superclass b of c (if any). second part (c', , i'm using ' "complement") contains fields "proprietary" of c (i.e. not being inherited b defined in c itself). important note created object not start on c', on b. combination of inherited fields new fields makes full object.

now, if b had had own superclass a, structure have been this:

+--------+-----------------+----------------------------+ |      |      b'         |              c'            + +--------+-----------------+----------------------------+ 

what important note here b == + b'. in other words, c doesn't need know a. cares immediate superclass b, hides internal structure.

to answer specific questions:

does constructor in case initialize variable , doesn't create concrete object of class?

in same way a, b, , c above structurally chained, chained during initialization. if weren't way, it's girl in original example had been born without being of other things know by definition. it's impossible. complete contradiction. so, construction process of both classes carried out, includes: initializing fields in "zero" value (null references , false boolean's), , executing 1 constructor (which can call other constructors).

in particular case, field i initialized, inheritance constructor executed, testinheritance's fields (if had had any) have been initialized, , testinheritance's constructor have been executed. how can tell constructors of each class? relatively simple. initiated new testinheritance(). indicating testinheritance's constructor doesn't have parameter (which exists; otherwise there compilation error). however, constructor not explicitly invoking constructor of superclass (through keyword super(...)). saw above, can not be, , compiler automatically inserts equivalent super(), i.e. call superclass's constructor without arguments (inheritance()). again, constructor exists, , good. output have been:

i in base class 0 in derived class 

constructors no parameters called "default constructors" 3 main reasons: - simple don't have parameters. - called automatically when constructor of subclass not explicitly call supeclass constructor. - provided automatically classes without constructor explicit written programmer. in case constructor nothing, , initialization takes place.

from have read far there 1 object created - of derived class has variable in it.

this not entirely correct. physically, it's true 1 object created, corresponds class used new (testinheritance), in case happens have no fields, irrelevant. in fact, both output lines printed. conceptually... mentioned this.

but time constructor of base class called , point in time constructor of derived class called how/where stored in memory?

when new testinheritance() executed, first thing happens, before constructors called, before initialization performed, memory allocated whole object. - memory "dirty" , that's why needs initialized. - there space testinheritance's fields, superclass' fields, , on. position of each field within object in memory know in advance.

so, before constructor of base class called there memory allocated i , other field, "dirty" (not initialized).

and case in base class abstract one.

there no difference. 1 can't create objects of abstract class themselves. create them part of (concrete) subclass. similar fact new human being can not born him/herself. it's either baby girl or baby boy.

i appreciate if can know happens in memory @ different points of time.

i hope did.

if have said fundamentally incorrect please let me know. want know how thing works.

nothing "fundamentally incorrect".