How to overwrite form method on form created by code?

On a form created in AOT you can rewrite methods by right click and overwrite. How can you do the same on the form which are created by X++ code?

For example. How to change close method so it will call info(“close”); before closing on this:

form = new Form();
formBuildDataSource = form.addDataSource("Table");
formBuildDesign = form.addDesign("Design");
form.design().caption("Caption");
…
args = new Args();
formRun = classfactory.formRunClass(args);
formRun.run();
formRun.detach();