• Post Reply Bookmark Topic Watch Topic
  • New Topic

Initialization and Inheritance  RSS feed

Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on an exercise to explain the process of initialization in
an inheritance context. This this the program I wrote and the explaination of the process. Will someone please comment if I am on the right track.
Thank you in advance.

class Manager {
private int i = 2;
protected int j;
Manager() {
System.out.println("i = " + i +", j = " + j);
j = 28;
private static int x1 = printInit("static Manager.x1 initialized");

static int printInit(String s) {
return 33;
class Foreman extends Manager {
private int k = printInit("Foreman.k initialized");
public Foreman() {
System.out.println("k = " + k);
System.out.println("j = " + j);
private static int x2 = printInit("static Foreman.x2 initialized");
public static void main(String [] args) {
System.out.println("Foreman constructor");
Foreman b = new Foreman();

public class Apprentice extends Foreman {
private int m = printInit("Apprentice.m initialized");
public Apprentice() {
System.out.println("m = " + (m=77));
System.out.println("j = " + j);
private static int x3 = printInit("static Apprentice.x3 initialized");
public static void main(String [] args) {
System.out.println("Apprentice constructor");
Apprentice b = new Apprentice();

// Process of class loading:
// When you first run the Foreman program, Java will first
// try to access the main() method. In the process Java
// loader notices that the Foreman class has a base class
// via the keyword "extends", which it then loads with or
// without an object of the base class been created. For
// example commenting out Foreman b = new Foreman in the
// main() will not affect compilation. The object of Foreman
// is an object of Manager in inheritance.

// In cases where the based class has its own based
// class (in this case Manager),and also other based clases
// up the heirarchy, that second based class (Manager) will // be loaded and so on.

// Next, the static initialization occurs and the root base
// class (the Manager class) is performed followed by the
// next subclass and so on.The continuation of this prcess
// is dependend on the proper initialization of the
// base-class members.

// From here on, all necessary class would have been loaded, // and object can be created. First. all the primitives in
// this object are set to default values of 0 and the object
// references are set to null. Then the constructor will be
// called. Constructor call is automatic. You may call
// the base-class constructor by using super.

// After the last base-class constructor completes, the
// instance variables are initialized followed by the rest
// of the body of the constructor is executed.

// The proces of the output:

// The program is loaded and main() method is called.

// In the process of loading the main() an "extends" keyword // is found and the base-class (Foreman) is loaded.

// While loading the second based-class (Foreman) another
// extends" is found and this second base-class is again
// loaded.

// Next the initialization.

// The static initialization in the root base class (in this
// case, Manager) is performed, and then the next derived
// class (Foreman) is performed. If all the static members
// are properly initialized, the necccesary classes would
// all been loaded and object can be created.

// Next the setting of primitives in this object to their
// default values of 0 and the object refernces to null (no
// object references in this case) are performed.

// Then the base-classes constructors are called in the
// order of Manager constructor followed by and the Foreman
// constructor.

// After the base-class constructors complete, instance
// variables are initialized in letter order. The order of
// initailzation is first the top base-class instance
// variables then the subclasses instance variable all in
// order of derivation.

// Finally, the remaining body of the last constructor is
// executed.

The Output:
static Manager.x1 initialized
static Foreman.x2 initialized
static Apprentice.x3 initialized
Apprentice constructor
i = 2, j = 0
Foreman.k initialized
k = 33
j = 28
Apprentice.m initialized
m = 77
j = 28
Press any key to continue . . .
Always! Wait. Never. Shut up. Look at this tiny ad.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!