Java initializing abstract classes
Solution 1
The line above is creating an anonymous subclass of SomeAbstractClass
, which will not be abstract
. Of course, this will work only if the base class has no abstract
methods to implement.
Actually, I cannot visualize an useful instance (besides "documentation" features, see the comment below) of the line above, unless you are implementing and/or overriding methods between curly braces. That is a quite common technique if the base class/interface happens to have few methods to implement and the implementation is simple. You can even refer to the final
variables of the surrounding method and parameters, thus making a closure.
Solution 2
You are creating an anonymous class which is a subclass of your abstract
class. Like was pointed out in comments, you are looking at an anonymous extends.
Something like follows would work if you had abstract
methods to implement:
MyAbstractClass someObjectOfThatClass = new MyAbstractClass(){
@Override
public void someAbstractMethod(){
}
}
You can do the same with interfaces as they can also contain abstract
methods. A practical example would be adding an ActionListener
to a JButton
:
myJButton.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
// code
}
});
Solution 3
Java gives you the ability to create anonymous subclasses inline. You often see this in the context of anonymous inner classes with Swing event handling, but there are many other applications as well.
In your example, you are creating a class that extends SomeAbstractClass
anonymously and assigning it to a SomeAbstractClass
reference. It would be just as if you created a separate class like this
public class SomeConcreteClass extends SomeAbstractClass {
}
and later did this
SomeAbstractClass variable = new SomeConcreteClass();
As noted by @Stefano, your approach only works if your anonymous concrete class has no abstract methods, which would be true because SomeAbstractClass
has no abstract methods.
user2651804
Updated on June 04, 2022Comments
-
user2651804 almost 2 years
Can someone explain this line of code for me?
SomeAbstractClass variable = new SomeAbstractClass() { };
This properly instantiaties and stores the abstract instance in the variable. What is happening? An anonymous class that extends the abstract class, maybe? Any keywords I can use to look up information about this? (the abstract class also happens to be generic if that has any relevance)
-
kiheru over 10 yearsI have seen it in mock objects in unit tests, where the real application should always use derived classes.
abstract
in that case would be more a documentation feature and a warning to avoid creating instances of the base class rather than necessary for the implementation. -
An SO User over 10 years@StefanoSanfilippo I thought a little bit of humor would work
-
Stefano Sanfilippo over 10 years@kiheru nice. I have added a pointer to your comment in the answer.
-
Vidya over 10 yearsTo refer to the variables in the surrounding method, they need to be
final
as well. And even if they are, the anonymous inner class only effectively makes a closure. It isn't really a closure.