I understand the abstract classes in java and, as an example of their use, the factory method is cited, and it is said that in this case we can not create a named instance of the class, which actually makes the syntax more meaningful, here is an example code:
interface Service{ void method1(); void method2(); } interface ServiceFactory{ Service getService(); } class Implementation1 implements Service{ private Implementation1(){ } public void method1(){ System.out.println("Implementetion1 print method1"); } public void method2(){ System.out.println("Implementetion1 print method2"); } public static ServiceFactory factory = new ServiceFactory(){ public Service getService(){ return new Implementation1(); } }; } class Implementation2 implements Service{ private Implementation2(){ } public void method1(){ System.out.println("Implementetion2 print method1"); } public void method2(){ System.out.println("Implementetion2 print method2"); } public static ServiceFactory factory = new ServiceFactory() { @Override public Service getService() { return new Implementation2(); } }; } class Factories{ public static void serviceConsumer(ServiceFactory fact){ Service s = fact.getService(); s.method1(); s.method2(); } } public class AbstractClass { public static void main(String[] arg){ Factories.serviceConsumer(Implementation1.factory); Factories.serviceConsumer(Implementation2.factory); } } It is not clear how this is better in practice than the following code:
interface Service{ void method1(); void method2(); } class Implementation1 implements Service{ public void method1(){ System.out.println("Implementetion1 print method1"); } public void method2(){ System.out.println("Implementetion1 print method2"); } } class Implementation2 implements Service{ public void method1(){ System.out.println("Implementetion2 print method1"); } public void method2(){ System.out.println("Implementetion2 print method2"); } } class Factories{ public static void serviceConsumer(Service s){ s.method1(); s.method2(); } } public class AbstractClass { public static void main(String[] arg){ Factories.serviceConsumer(new Implementation1()); Factories.serviceConsumer(new Implementation2()); } } In the first case, in Factories.serviceConsumer we pass a specific field of a particular class, why not send the object immediately as in the second case, and in the second case, you need to write less code.