java - How to call a non static abstract method from another class -
this header of abstract class
public abstract class recursivego extends jframe implements actionlistener
i want take method class, non static, , call in main method of driver of class. have not yet made driver, know if extend class driver , make driver subclass come error because not overriding actionlistener. how can call non static method abstract class in non abstract driver?
if there useful non-static method depends on subset of class (i.e. doesn't depend on actionlistener), entire class has stay abstract, sounds there's clean line of separation there. refactor class extract subset, , make available driver.
public abstract class recursivego extends jframe implements actionlistener { private final usefulsubset usefulsubset; public recursivego() { usefulsubset = new usefulsubset(); } // static, can address recursivego.usefulsubset. // maybe extends jframe, too, , if made top-level class // recursivego further subclass subset. public static class usefulsubset { void methodcall {} } } class yourdriver { public static void main(string[] args) { recursivego.usefulsubset instance = new recursivego.usefulsubset(); instance.methodcall(); } }
of course, maybe useful subset stand on own top-level class, better. code malleable, , if it's not shaped way like, feel free change shape.
as ajb notes in comments, see if making method static worthwhile. favor dependency injection , mocking frameworks, i'm not fan of moving mountains making methods static, of course simple solution problem.
Comments
Post a Comment