File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes extending inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "extending inner class " Watch "extending inner class " New topic
Author

extending inner class

Shakti Sharma
Ranch Hand

Joined: Dec 19, 2000
Posts: 60
I am extending inner class in other inner sub class class but enclosing class of the inner subclass is not a subclass of Outer and i am passing explicit reference to an object of Outer to a constructor of Inner subclass. Problem is my member variables of inner subclass are not getting initialized and also initialization block is not executed even though object of inner subclass class is created successfully
Can any one tell me what is wrong in this code
code:
--------------------------------------------------------------------------------
public class outer {
public class inner {
}
}
-------------------------------------------------------------------------
public class eouter {
public class iinner extends outer.inner {
int z= 10;
{System.out.println("I am called");}
public iinner(outer out) {
out.super();
} }public static void main(String args[]) {outer out = new outer();eouter eout = new eouter();iinner inn = eout.new iinner(out);System.out.println("z = " + inn.z);}}
--------------------------------------------------------------------------------
I am expecting out put like
I am called
z = 10
but i am getting
z = 0
only
Son Le
Greenhorn

Joined: Jan 15, 2001
Posts: 21
Shakti,
I'm getting what you expected.
I'm called.
z=10
I'm using Java1.2
Hungson Le


-LHS
Suresh Selvaraj
Ranch Hand

Joined: Nov 14, 2000
Posts: 104
Hi,
I tried your code and is giving the output you expected!!!
Here it is :
class outer {
public class inner { }
}

public class eouter {
public class iinner extends outer.inner {
int z= 10;
{
System.out.println("I am called");
}

public iinner(outer out)
{
out.super();
}
}
public static void main(String args[])
{
outer out = new outer();
eouter eout = new eouter();
iinner inn = eout.new iinner(out);
System.out.println("z = " + inn.z);
}
}
"I am called"
z=10
- Suresh Selvaraj,
[ www.jtips.net ]


Suresh Selvaraj, (author of JQuiz)<br />SCJP2<br /><a href="http://www.decontconsulting.com" target="_blank" rel="nofollow">www.decontconsulting.com</a>
Ishaan Mohan
Ranch Hand

Joined: Jan 20, 2001
Posts: 115
I am not able to understand the flow of this code. Can anybody please explain it to me.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: extending inner class
 
Similar Threads
inner class
extending Inner classes
use of Outer.this
Inner class
Something weird about static imports