Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Argument-local inner class in Java

Options
  • 04-06-2009 8:57pm
    #1
    Registered Users Posts: 260 ✭✭


    Still studying for SCJP. This question is shorter, though!

    Given the following code, which all compiles just fine, my question is how I access the overridden doStuff() method:
    class MyWonderfulClass {
    	void go(){
    		Bar b = new Bar();
    		
    		b.doStuff(new Foo(){
    			public void foof(){
    				System.out.println("Doing foofy stuff");
    			}//end foof() method
    		});//end new inner class
    	}//end go()
    }//end class
    
    interface Foo{
    	void foof();
    }
    
    class Bar{
    	void doStuff(Foo f){
    		System.out.println("doStuff() not overridden!!");
    	}
    }
    

    I did attempt to do the following, but it only calls the original, not the overridden version of doStuff():
    public class MWCTester {
    	public static void main(String [] args){
    		MyWonderfulClass mwc = new MyWonderfulClass();
    		mwc.go();    //attempting to call overridden doStuff() method 
    	}
    }
    


Comments

  • Moderators, Science, Health & Environment Moderators Posts: 10,079 Mod ✭✭✭✭marco_polo


    It is not possible to call an overidden method in java.


  • Registered Users Posts: 260 ✭✭pdebarra


    marco_polo wrote: »
    It is not possible to call an overidden method in java.

    I disagree. Given a class Dog, as below, and a class Terrier, which extends Dog, the Terrier class overrides Dog's bark() method. When we call bark() on a Terrier, we call the overridden method.
    class Dog{
    	public void bark(){
    		System.out.println("Woof woof");
    	}
    }
    
    class Terrier extends Dog{
    	public void bark(){
    		System.out.println("Yap yap");
    	}
    }
    
    public class DogTester{
    	public static void main(String[] args){
    		Dog d1 = new Dog();
    		Terrier t1 = new Terrier();
    		
    		d1.bark();
    		t1.bark();
    	}
    }
    

    What I'm trying to find out is how to cause the overridden method foof() from the anonymous inner class within MyWonderfulClass, above, to execute.


  • Registered Users Posts: 5,618 ✭✭✭Civilian_Target


    It's not clear to me what you're trying to do. Where's the overwrite?

    If you declare an annoymous inner class in a method, you can only execute it from that method. (Unless you use some nasty reflection tricks).


  • Registered Users Posts: 260 ✭✭pdebarra


    Ah! Got it. Silly me. I hadn't properly understood what I was at.

    I should have referred to the overridden foof() method, instead of the overridden doStuff() method.

    In any case, the Bar class needed one minor modification in order to actually call the new foof() method:
    class Bar{
    	void doStuff(Foo f){
    		f.foof(); 	//new line inserted to ensure that 
    					//foof() is actually called on 
    					//the new Foo object.
    	}
    }
    

    This will indeed print
    Doing foofy stuff
    

    Many thanks for replies.


  • Moderators, Science, Health & Environment Moderators Posts: 10,079 Mod ✭✭✭✭marco_polo


    pdebarra wrote: »
    I disagree. Given a class Dog, as below, and a class Terrier, which extends Dog, the Terrier class overrides Dog's bark() method. When we call bark() on a Terrier, we call the overridden method.

    Oops I didn't fully read your post fully :o, so you were not trying to do what I asumed you were.

    I was referring to the overidden method as being the base class version, and the subclass method as the overiding method which is pretty common general usage. Strictly speaking though I guess all versions of the method are the overidden method.

    Glad you got it sorted anyway :)


  • Advertisement
  • Registered Users Posts: 260 ✭✭pdebarra


    Hmmm... Yes, bark() in the Terrier class is the overriding method, and bark() in Dog is the overridden method...

    Ah, but you can still call an overridden method :D:
    super.bark();
    

    oughter do it!

    EDIT: Assuming that this code is in Terrier, not Dog! Think before you post, PdeB :mad:


Advertisement