37

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?

TylerH
  • 20,799
  • 66
  • 75
  • 101
satheesh.droid
  • 29,947
  • 10
  • 34
  • 34
  • 11
    Among other things, Chuck Norris can instanciate abstract classes: http://www.ithoughts.de/chuck-norris-programmer-facts ;-) – Lukas Eder Jan 02 '11 at 16:37
  • Chuck Norris can do a lot of other tricks in programming: http://chucknorrisfacts.co.uk/fact/category/4/ – Artur Kedzior Jan 27 '11 at 10:54
  • LocationProvider is not an abstract class i think , its just a normal public class as you can see here. http://developer.android.com/reference/android/location/LocationProvider.html You might be telling this cause they used abstract word in the class documentation. – Vins Jan 21 '16 at 10:42

4 Answers4

87

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.

kiritsuku
  • 52,967
  • 18
  • 114
  • 136
  • 2
    can we have function definition inside abstract class? – satheesh.droid Jan 02 '11 at 16:48
  • 1
    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. – kiritsuku Jan 02 '11 at 17:57
  • @sschaef, can you explain why the call to `p.printSomethingOther` works fine from inside the class but not from outside? – SexyBeast Oct 13 '14 at 10:29
  • 3
    @Cupidvogel: because it is not public/not known to type `Printer`. – kiritsuku Oct 13 '14 at 11:31
  • What word would you describe this as, if not "instantiation"? – James Wierzba Sep 10 '15 at 16:26
  • @JamesWierzba it is instantiation, just not of the abstract class but the anonymous subclass. – kiritsuku Sep 10 '15 at 17:15
  • @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? – AnV Sep 15 '16 at 11:58
  • 1
    @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](https://stackoverflow.com/questions/4811678/defining-an-abstract-class-without-any-abstract-methods)) – h4nek May 31 '19 at 11:04
23

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.

Community
  • 1
  • 1
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • Can you please elaborate on factory pattern or give some link about that? – satheesh.droid Jan 02 '11 at 16:36
  • @satheesh.droid, here's an example: http://www.javabeat.net/tips/18-factory-pattern-design-patterns-in-javaj.html – Darin Dimitrov Jan 02 '11 at 16:36
  • 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? – theyuv Mar 19 '19 at 09:08
5

No, abstract class can never be instantiated.

Prateek Jain
  • 1,234
  • 14
  • 24
0

According to others said, you cannot instantiate from abstract class. but it exist 2 way to use it. 1. make another non-abstact class that extends from abstract class. So you can instantiate from new class and use the attributes and methods in abstract class.

    public class MyCustomClass extends YourAbstractClass {

/// attributes, methods ,...
}
  1. work with interfaces.
Haniyeh Khaksar
  • 774
  • 4
  • 20