aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt regarding Innerclasses Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt regarding Innerclasses" Watch "Doubt regarding Innerclasses" New topic
Author

Doubt regarding Innerclasses

vatsalya rao
Ranch Hand

Joined: Feb 14, 2007
Posts: 63
class A {
protected class InnerA
{
public InnerA()
{
System.out.println("A.InnerA()");
}

public void f()
{
System.out.println("A.InnerA.f()");
}
}//innerA end

private InnerA y = new InnerA();

public A()
{
System.out.println("New A()");
}

public void insertYolk(InnerA yy)
{
y = yy;
}

public void g()
{
y.f();
}
}//class A ending

class B extends A
{
public class InnerB extends A.InnerA
{
public InnerB()
{
System.out.println("B.InnerB()");
}

public void f()
{
System.out.println("B.InnerB.f()");
}
}//Inner B ending

public B()
{
insertYolk(new InnerB());
}

}//class B ending

public class MainClass
{
public static void main(String[] args)
{
A e2 = new B();
e2.g();
}
}//end of mainClass


I expected o/p as

newA()
B.innerB()
A.innerA.f()

because when "A e2 = new B()" is executed

the constructor A() is executed first printing
"new A()"

then B() is called,which creates an instance of 'Inner B" thus printing

"B.inner B()"

Coming to second line in 'main" when it executes 'e2.g"

g() in A is called,which inturn calls the method "f()" on innerclassA instance and printing

A.InnerA.f()



But the actual o/p is

A.InnerA()
New A()
A.InnerA()
B.InnerB()
B.InnerB.f()


Can anybody please explain this program's flow?
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
Howdy!




First line in the main method is
A e2 = new B();

so the first thing to do is to create a new B();
The constructor of B seems to start with
insertYolk(new InnerB());


but there is also the invisible call:
super();

before, so before any yolk action is done, the constructor of A runs saying:
super(); // invisible call of class Object's constructor, but no output, so I'll ignore this
and then
System.out.println("New A()");

This seems to be the first line printed, but again something was forgotten:
The line
private InnerA y = new InnerA();

also belongs to the constructor, and the initialization of variables comes before the typed-in code in the brackets of the constructor, so we are still with no output and have to do
a new InnerA();


Constructor of InnerA runs. InnerA has also only Object as super class, so you can ignore the call of super() again - at least regarding output.

InnerA's constructor says:
System.out.println("A.InnerA()");

And really, this is the first line printed. The constructor is over, and we jump back were we came from, the constructor of A and now:
System.out.println("New A()");
New A() is printed.

We came from the constructor of B, so back there and we have a method call:
insertYolk(new InnerB());

You have to construct an innerB object now...
I think, from here, you can follow the rest of the flow by yourself.


Yours,
Bu.


all events occur in real time
vatsalya rao
Ranch Hand

Joined: Feb 14, 2007
Posts: 63
Hello Bu,

Thank you for the in-detail explanation.

But still doubt with the line e2.g() how it is printing B.innerB.f()


Because when e2.g() is called,g() in A is called [Because class B is not having the method g()] and which inturn calls
y.f().

AS y is of type 'Inner class A" ,f() in "inner class A" should be executed ,and should print "A.iinerA.f()

How it is printing B.innerB.f().

If I sound silly,iam very sorry.Really I tried hard to get it by myself,But atlast landed here again for the clarificatio.
Burkhard Hassel
Ranch Hand

Joined: Aug 25, 2006
Posts: 1274
e2.g(); // calls:
y.f();

the y is of type InnerA but the object that it refers to is an InnerB object that was created during the process I quite short described at the end of my first post. It was in the line
insertYolk(new InnerB());

So we have this innerB object already and now the f() method is called with it.
In InnerB method f() is overridden as


And this is just what it prints.

Little hint:
To seperate what prints what, maybe you should put a
System.out.println("---------");

between the first and second line of the main method.
You'll see that the line
e2.g();

just prints "B.InnerB.f()".



Yours,
Bu.
 
 
subject: Doubt regarding Innerclasses