Results 1 to 4 of 4
Thread: Inner class method call
- 11-11-2010, 08:49 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 4
- Rep Power
- 0
Inner class method call
How can we access a anonymous inner class method or field* that is not described in interface? or is it possible without adding the method to the interface?Java Code:interface I { void ishow(); } public class An { I getI() { return new I() { public void ishow() { } void print() { } //(*) // how can we access this method? }; } public static void main(String[] args) { (new An()).getI().ishow(); // ! (new An()).getI()).print(); - obviously won't work (compilation error) } }
*I guess only static final fields are valid inside an interface bodyLast edited by d915172; 11-11-2010 at 08:52 PM.
- 11-11-2010, 09:09 PM #2
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,545
- Rep Power
- 11
Since An declares getI() to return an instance of I, the compiler will wisely forbid you to do anything with that instance other than what an I promises to be able to do.
You could have another class (or interface) AnI which implements (or extends) I and has the print() method. getI() could be declared to return an instance of AnI. The important thing is that the caller of getI() - which could be someone else's code in some other totally unrelated package written years later (*) - has a publically exposed API, such as that provided by AnI, to work to.
Edit: * OK, getI() has only default accessibility, but the point remains.Last edited by pbrockway2; 11-11-2010 at 09:14 PM.
- 11-11-2010, 09:10 PM #3
Senior Member
- Join Date
- Feb 2009
- Posts
- 303
- Rep Power
- 5
You can but its tricky and ugly.
It would be a better approach to either create a class that implements the interface and provides the extra method, or create a new interface which extends the current interface and add the method to the new interface.Java Code:public interface Interface { public void show(); } public class Test { public Interface getInterface() { return new Interface(){ public void show() { System.out.println("Show"); } public void print() { System.out.println("print"); } }; } public static void main(String[] args) { Test test = new Test(); Interface inter = test.getInterface(); inter.show(); try { inter.getClass().getMethod("print", new Class[]{}).invoke(inter, new Object[]{}); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (SecurityException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (NoSuchMethodException e) { e.printStackTrace(); } } }
Java Code:public class InterfaceObject implements Interface { public void show() { System.out.println("Show"); } public void print() { System.out.println("Print"); } } public interface SubInterface extends Interface { public void print(); }
- 11-11-2010, 09:40 PM #4
Member
- Join Date
- Nov 2010
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
Trying to call a method from sub class
By TheNewGuy in forum New To JavaReplies: 4Last Post: 10-17-2010, 07:08 AM -
how call from inner class(anonymous or not), a method of parent class?
By lse123 in forum AWT / SwingReplies: 2Last Post: 05-01-2010, 08:59 AM -
How to call a method from another class?
By jboy in forum New To JavaReplies: 8Last Post: 09-09-2009, 07:29 AM -
How to call a class within a method
By Manfizy in forum New To JavaReplies: 3Last Post: 03-19-2009, 12:34 PM -
[SOLVED] Call method from another class name
By antgaudi in forum New To JavaReplies: 3Last Post: 10-15-2008, 12:33 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks