Can we instantiate an abstract class directly?
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.
satheesh.droid
Updated on June 08, 2020Comments
-
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 callinggetProvider()
function in theLocationManager
class:LocationManager lm = getSystemService(Context.LOCATION_PROVIDER); LocationProvider lp = lm.getProvider("gps");
How is the abstract class instantiate here?
-
satheesh.droid over 13 yearsCan you please elaborate on factory pattern or give some link about that?
-
Darin Dimitrov over 13 years@satheesh.droid, here's an example: javabeat.net/tips/…
-
satheesh.droid over 13 yearscan we have function definition inside abstract class?
-
kiritsuku over 13 yearsYou 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 over 9 years@sschaef, can you explain why the call to
p.printSomethingOther
works fine from inside the class but not from outside? -
kiritsuku over 9 years@Cupidvogel: because it is not public/not known to type
Printer
. -
James Wierzba over 8 yearsWhat word would you describe this as, if not "instantiation"?
-
kiritsuku over 8 years@JamesWierzba it is instantiation, just not of the abstract class but the anonymous subclass.
-
AnV over 7 years@sschaef Is it allowed for an
abstract
class (in this casePrinter
) to have only concrete method(s) [method(s) with definition]? Any class must have at least oneabstract
method to becomeabstract
right? -
theyuv about 5 yearsWhat'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 thecreateButton
method in a separateButtonFactory
class rather than in the abstractButtton
class itself? -
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)