| Author |
Can we create a Inner Class for a INNER Class?
|
Justin Smith
Greenhorn
Joined: Jul 24, 2008
Posts: 19
|
|
|
Hi I have one doubt regarding Inner class... This is different than the other topic though... Can I create an Inner Class for a INNER class?? what will happen if I create and what is the use of it???
|
 |
Baseet Ahmed
Ranch Hand
Joined: Dec 18, 2006
Posts: 223
|
|
Yes,you can. Regards Baseet Ahmed
|
 |
Justin Smith
Greenhorn
Joined: Jul 24, 2008
Posts: 19
|
|
Hi Baseet, I tried creating one and was able to create it, but i wish to know is there any real world scenario which would need a scenario like this??? Here is my sample program.... import java.util.*; import java.lang.*; public class MyOuter implements java.io.Serializable { private int x = 6; private String name = "Humpty Dumpty"; public MyOuter(){System.out.println("Calling the Outermost Constructor");} public MyOuter(String arg){} public double seeCurrent() { float f = 3.1416f; Double d = Double.valueOf(f*x); return d; } class MyInner { public MyInner(){System.out.println("Calling the MyInner Constructor");} public void seeOuter() { System.out.println("The value of the 'x' is :"+x); } class MyInnerMost extends java.lang.Thread { public MyInnerMost(){System.out.println("Calling the MyInnerMost Constructor");} public void seeOuterMost() { System.out.println("The 'name' selected for $1 million dollar is:"+name); } } } public static void main(String[] are) { System.out.println("Before creating any instance in the main method"); Double value = null; MyOuter mo = new MyOuter(); value = mo.seeCurrent(); System.out.println("The value of the return value is :"+value); MyOuter.MyInner mi = new MyOuter().new MyInner(); mi.seeOuter(); MyOuter.MyInner.MyInnerMost mim = new MyOuter().new MyInner().new MyInnerMost(); mim.seeOuterMost(); } } The output is : --------------- The value of the return value is :18 Calling the Outermost Constructor Calling the MyInner Constructor The value of the 'x' is :6 Calling the Outermost Constructor Calling the MyInner Constructor Calling the MyInnerMost Constructor The 'name' selected for $1 million d Press any key to continue . . .
|
 |
Sumit Bisht
Ranch Hand
Joined: Jul 02, 2008
Posts: 302
|
|
Sure! you can do so.But as you observe, this programming practice is a sort of obfuscating the logic.Even you can do various other combinations with method-local/argument specific local classes(and their sub-classes). In actual programming development if you feel the need, you should reconsider your design/programming practice(well I am not sure here).Mainly we need inner classes to handle event driven logic in GUI but other than that, OOP in my opinion eliminates this need for code cluttering/repetitive logic.
|
 |
Justin Smith
Greenhorn
Joined: Jul 24, 2008
Posts: 19
|
|
Hi Sumeet, Thanks for your reply !!!
|
 |
 |
|
|
subject: Can we create a Inner Class for a INNER Class?
|
|
|