Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Anonymous class definition based on interface... maybe?

Tags:

java

I saw this Java snippet in the book Spring in Action, but I'm not familiar with the language construct.

new RowMapper() {
  public Object mapRow() throws SQLException, DataAccessException {
    Motorist motorist = new Motorist();
    motorist.setId(rs.getInt(1));
    motorist.setEmail(rs.getString(2));
    motorist.setPassword(rs.getString(3));
    motorist.setFirstName(rs.getString(4));
    motorist.setLastName(rs.getString(5));
    return motorist;
  }
}

According the Spring documentation, RowMapper is an interface. It looks to me like an anonymous class definition based on the RowMapper interface. The new keyword is a little confusing, making me wonder if this also creates one instance of the anonymous class. I would guess yes, because if the class has no name, how will you ever create an instance after the line that defines it?

Can anyone confirm my guesses that:

  • this is an anonymous class definition based on the RowMapper interface, and
  • it creates a single instance of that class?
like image 751
Mike M. Lin Avatar asked Mar 03 '11 17:03

Mike M. Lin


People also ask

How to implement interface in anonymous class in C #?

How to implement interface in anonymous class in C#? No, anonymous types cannot implement an interface. We need to create your own type. Anonymous types provide a convenient way to encapsulate a set of read-only properties into a single object without having to explicitly define a type first.

What is @anonymous class in Java?

Anonymous class usually extends a subclass or implement an interface Functional Interface is simply an interface that has exactly one abstract method. For example, the interface Animal is a Functional Interface. You can annotate functional interfaces with @FunctionalInterface

What are the different types of anonymous inner classes?

Types of anonymous inner class : Based on declaration and behavior, there are 3 types of anonymous Inner classes: Anonymous Inner class that extends a class : We can have an anonymous inner class that extends a class.For example,we know that we can create a thread by extending a Thread class.

What is the difference between local and anonymous classes?

Like local classes, anonymous classes can capture variables; they have the same access to local variables of the enclosing scope: An anonymous class has access to the members of its enclosing class.


2 Answers

This is an anonymous class definition based on the RowMapper interface

That's precisely what it is.

It creates a single instance of that class?

Yep. That's correct.

like image 181
aioobe Avatar answered Sep 28 '22 04:09

aioobe


That code is implementing the interface in an anonymous way.

The syntax would be similar to:

Runnable runnable = new Runnable() {
    public void run() {
    }
};

Note the semicolon at the end of the declaration. Here the runnable object, though holds the reference to the Runnable interface actually contains the implemented object. That's runtime polymorphism for you!

like image 39
adarshr Avatar answered Sep 28 '22 06:09

adarshr