1

I am reading a book in which author used a code like this

public class Pool<T> { public interface PoolObjectFactory<T> { public T createObject(); } private final List<T> freeObjects; private final PoolObjectFactory<T> factory; private final int maxSize; public Pool(PoolObjectFactory<T> factory, int maxSize) { this.factory = factory; this.maxSize = maxSize; this.freeObjects = new ArrayList<T>(maxSize); } //end of constructor } //end of class Pool<T> 

Then he used code something like this

PoolObjectFactory<KeyEvent> factory = new PoolObjectFactory<KeyEvent>() { @Override public KeyEvent createObject() { return new KeyEvent(); } //end of createObject() }; keyEventPool = new Pool<KeyEvent>(factory, 100); 

I want to ask at the line PoolObjectFactory<KeyEvent> factory = new PoolObjectFactory<KeyEvent>() {..}; he didn't say implements PoolObjectFactory. Why? When you use interface then you use implements keyword?

Thanks

5 Answers 5

5

The author is using a special construct called an anonymous class, which is defined using the following syntax:

new interface-name () { class-body } 
Sign up to request clarification or add additional context in comments.

Comments

1

You only need implements when you are defining a named class.
In this case the author is using an anonymous (unnamed) inner class which implements an interface.

Edit: For background, something a lot of people don't realise/appreciate is that the compiler will generate a .class file for anonymous inner classes. e.g. the following produces a Fred.class file and a Fred$1.class file:

public class Fred { public void sayHelloFred() { Object o = new MouseAdapter() { }; } } 

All these .class files still need to be shipped and loaded at runtime, just like if they were named classes.

Comments

0

You are creating an anonymous class here. No need for the implements keyword. Note that in this case you use new PoolObjectFactory and this clearly indicates the class you are implementing - no need to indicate it once more using implements.

On the other hand if you are creating a subclass you need to state which is it's parent interface and then you need the implements keyword.

5 Comments

You mean to say the statement new PoolObjectFactory<KeyEvent>() {..} means create and simultaneously instantiate a new class named PoolObjectFactory, that implements the interface PoolObjectFactory. Means i can use new Basit<AnyType>(){...}. But then Basit class only implements PoolObjectFactory if in the class body i override the method. If i don't override the method then it means that i am just creating and instantiating a new class but it is not implementing any interface. And finally assign the newly created class instance to variable Basit<AnyType> factory = . Am i right ?
No, not quite. First you create an anonymous class - that is a class with no name but then, yes you do instantiate it. You can not instantiate a class that has non-implemented methods so I don't quite understand the second part of your statement.
I mean the anonymous class implements the interface because i override the method in the class body. Suppose i used something like this PoolObjectFactory<KeyEvent> factory = new PoolObjectFactory<KeyEvent>() {public int i = 2;}; See no override method in the class body. Is this class also implementing PoolObjectFactory interface? Can i use constructor on the annonymous class like `{public int i = 2; public PoolObjectFactory() {}}}; ?
You can not instantiate the anonymous class without overriding all methods from the interface. You can have more complicated logic like variables('i' in your example), but usually I would use anonymous class only for simpler logic and create a separate named class for the more complicated case.
HHmm ok. Mean in my case it is creating and instantiating an annonymous class that implements the interface, although class has no name but the annnymous class instance is assigning to the variable. Now am i understood right ?
0

The author uses an anonymous class. In such case, a class is created without an identifier and only one instance is created and assigned to the factory variable.

Comments

0

The implements keyword is used when defining a class to indicate that the class will meet the requirements of the interface. in the line in question, the author instantiates an anonymous class.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.