I am trying to use template design pattern so I use abstract class to define my algorithm like this:
abstract class MyTemplate
{
public void execute()
{
//... do something
doSomething();
}
public abstract void doSomethig();
}
In my code I will create an instanceof MyTemplate everytime like this:
MyTemplate cleanUp = new MyTemplate()
{
public void doSomething()
{
// execute cleanup
}
}
cleanUp.execute();
Is creating a object out of abstract class expensive for JVM?
Thanks,
Sean Nguyen
You’re not really creating an abstract class instance. You’re creating an instance of a concrete class which happens to have no name in your code, created by the Java compiler.
The JVM doesn’t really know or care about that – so it’s only as expensive as creating an instance of any other class which happens to be a subclass of an abstract class. So don’t sweat it 🙂