Can we instantiate an abstract class directly? [duplicate] Can we instantiate an abstract class directly? [duplicate] android android

Can we instantiate an abstract class directly? [duplicate]


You can't directly instantiate an abstract class, but you can create an anonymous class when there is no concrete class:

public class AbstractTest {    public static void main(final String... args) {        final Printer p = new Printer() {            void printSomethingOther() {                System.out.println("other");            }            @Override            public void print() {                super.print();                System.out.println("world");                printSomethingOther(); // works fine            }        };        p.print();        //p.printSomethingOther(); // does not work    }}abstract class Printer {    public void print() {        System.out.println("hello");    }}

This works with interfaces, too.


No, you can never instantiate an abstract class. That's the purpose of an abstract class. The getProvider method you are referring to returns a specific implementation of the abstract class. This is the abstract factory pattern.


No, abstract class can never be instantiated.