aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Anonymous Inner class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Anonymous Inner class" Watch "Anonymous Inner class" New topic
Author

Anonymous Inner class

Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 356
I am having following code:



In line 4, anonymous class is passed as argument. I wanted to know how is the method foof() of that class getting called.


SCJP 1.4, SCWCD1.4, OCA(1Z0-007)
Ahsan Bagwan
Ranch Hand

Joined: Oct 05, 2010
Posts: 158
Foo is not a class; its an interface.

I am not certain but I'll bite. You are creating an anonymous inner class which implements the interface 'Foo' and also provides the implementation of the method foof() (because it has a contract with Foo when it uses `implements`although there is no `implements` and is implied in this case).
Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1018
    
  15

I am not certain but I'll bite. You are creating an anonymous inner class which implements the interface 'Foo' and also provides the implementation of the method foof() (because it has a contract with Foo when it uses `implements`).


I'm certain that you're right.

This is one case in which we can type a new <InterfaceName> and the compiler does not complain.

I will just add that you are not only creating an on the spot implementation ( subclass ) of Foo, but also creating an instance of this subclass at the same time and passing this instance as an argument to the method doStuff.

Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 356
Actually my question arises from the fact because i fail to understand the following code:



From my main, i call getEmployeeById method, which in turn calls jdbcTemplate.queryForObject. In this method the 2nd argument is anonymous class implementing RowMapper, which has method mapRow. How is the method mapRow being called.
Nam Ha Minh
Ranch Hand

Joined: Oct 31, 2011
Posts: 498

Naresh Chaurasia wrote:
From my main, i call getEmployeeById method, which in turn calls jdbcTemplate.queryForObject. In this method the 2nd argument is anonymous class implementing RowMapper, which has method mapRow. How is the method mapRow being called.

Generally anonymous class' methods are called by the calling method's implementation.
In your case, that mapRow() method will be used by the calling method queryForObject() because it known that the passed argument is of type RowMapper which defines the method mapRow().


PM Me If You Want to get Urgent Help on Java Programming
Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 356
Nam Ha Minh wrote:
Generally anonymous class' methods are called by the calling method's implementation.
In your case, that mapRow() method will be used by the calling method queryForObject() because it known that the passed argument is of type RowMapper which defines the method mapRow().



But how is the call made by to mapRow() i.e. how and when it is invoked.
Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1018
    
  15
But how is the call made by to mapRow() i.e. how and when it is invoked.


I'm not sure, but isn't this part of your question unrelated to the SCJP/OCPJP forum questions? Wouldn't it'd better be in the jdbc and/or frameworks forum.

A google search on 'how RowMapper works with jdbcTemplate' showed me following links where this is sort of explained. A couple of links I found are -

http://www.studytrails.com/frameworks/spring/spring-jdbc-template.jsp
http://www.coderanch.com/t/447286/Spring/explanation-RowMapper-mapRow-method
http://www.coderanch.com/t/570063/Spring/RowMapper-callback-interface-mapRow-pull

And is this a crosspost from
http://stackoverflow.com/questions/18534021/how-rowmapper-can-be-an-anonymous-class

Chan.
Naresh Chaurasia
Ranch Hand

Joined: May 18, 2005
Posts: 356
Naresh Chaurasia wrote:
But how is the call made by to mapRow() i.e. how and when it is invoked.


Thanks chan, I got my answer. This is what i had to do.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Anonymous Inner class