Can we instantiate an abstract class directly?

115,449

Solution 1

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.

Solution 2

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.

Solution 3

No, abstract class can never be instantiated.

Share:
115,449
satheesh.droid
Author by

satheesh.droid

Updated on June 08, 2020

Comments

  • satheesh.droid
    satheesh.droid almost 4 years

    I have read we can only instantiate an abstract class by inheriting it, but we cannot instantiate it directly.
    However, I saw we can create an object with the type of an abstract class by calling a method of another class.
    For example - LocationProvider is an abstract class, and we can instantiate it by calling getProvider() function in the LocationManager class:

    LocationManager lm = getSystemService(Context.LOCATION_PROVIDER);
    LocationProvider lp = lm.getProvider("gps");
    

    How is the abstract class instantiate here?

  • satheesh.droid
    satheesh.droid over 13 years
    Can you please elaborate on factory pattern or give some link about that?
  • Darin Dimitrov
    Darin Dimitrov over 13 years
    @satheesh.droid, here's an example: javabeat.net/tips/…
  • satheesh.droid
    satheesh.droid over 13 years
    can we have function definition inside abstract class?
  • kiritsuku
    kiritsuku over 13 years
    You can create methods inside anonymous classes, but you can only call these methods inside of the anonymous class. See the code in my answer, I edited it.
  • SexyBeast
    SexyBeast over 9 years
    @sschaef, can you explain why the call to p.printSomethingOther works fine from inside the class but not from outside?
  • kiritsuku
    kiritsuku over 9 years
    @Cupidvogel: because it is not public/not known to type Printer.
  • James Wierzba
    James Wierzba over 8 years
    What word would you describe this as, if not "instantiation"?
  • kiritsuku
    kiritsuku over 8 years
    @JamesWierzba it is instantiation, just not of the abstract class but the anonymous subclass.
  • AnV
    AnV over 7 years
    @sschaef Is it allowed for an abstract class (in this case Printer) to have only concrete method(s) [method(s) with definition]? Any class must have at least one abstract method to become abstract right?
  • theyuv
    theyuv about 5 years
    What's the motivation for placing the create[Object] method in a separate [Object]Factory class rather than in the abstract [Object] class itself? To use the example in your link: What's the motivation for placing the createButton method in a separate ButtonFactory class rather than in the abstract Buttton class itself?
  • h4nek
    h4nek almost 5 years
    @AnV For a class to be abstract, you just need to define it abstract. It can then have 0 abstract methods along with any number of concrete methods. (see: this SO question)