Results 1 to 4 of 4
  1. #1
    d915172 is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Question Inner class method call

    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)
      }
    }
    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?

    *I guess only static final fields are valid inside an interface body
    Last edited by d915172; 11-11-2010 at 08:52 PM.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    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.

  3. #3
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    304
    Rep Power
    6

    Default

    You can but its tricky and ugly.
    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();
    		}
    	}
    }
    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 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();
    }

  4. #4
    d915172 is offline Member
    Join Date
    Nov 2010
    Posts
    4
    Rep Power
    0

    Default

    StormyWaters, I checked your code, it works perfect (just needed to add one import) and answers my question!

    Thanks a lot!

Similar Threads

  1. Trying to call a method from sub class
    By TheNewGuy in forum New To Java
    Replies: 4
    Last Post: 10-17-2010, 07:08 AM
  2. Replies: 2
    Last Post: 05-01-2010, 08:59 AM
  3. How to call a method from another class?
    By jboy in forum New To Java
    Replies: 8
    Last Post: 09-09-2009, 07:29 AM
  4. How to call a class within a method
    By Manfizy in forum New To Java
    Replies: 3
    Last Post: 03-19-2009, 12:34 PM
  5. [SOLVED] Call method from another class name
    By antgaudi in forum New To Java
    Replies: 3
    Last Post: 10-15-2008, 12:33 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •