Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Instantiate Java Abstract class? [duplicate]

Relatively new Java programmer and I've been taught that you can't create an instance of an abstract class. I've also done a little research and I learned that in most cases when it appears an abstract class is being created, it is actually an anonymous subclass. But here is the issue I ran into:

Java's URL class has a method openConnection that returns a URLConnection. URLConnection is an abstract class, and the Java documentation also lists all of its subclasses as abstract..... so I'm really lost. What's being returned?

like image 611
Ausome Avatar asked May 01 '15 14:05

Ausome


People also ask

Can we instantiate abstract class Java?

Abstract classes cannot be instantiated, but they can be subclassed. When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract .

How many times can you instantiate an abstract class?

Abstract class, we have heard that abstract class are classes which can have abstract methods and it can't be instantiated. We cannot instantiate an abstract class in Java because it is abstract, it is not complete, hence it cannot be used.

Can we clone abstract class?

clone() will not work on interfaces and abstract classes. The only way to use the Object. clone() method is if the class of an object is known, i.e., we cannot access the clone() method on an abstract type since most interfaces and abstract classes in Java do not specify a public clone() method.

Can abstract class be multiple inherited?

A class can inherit from multiple abstract classes.


2 Answers

What is being returned is a non-abstract subclass that you won't find in the API documentation, for example sun.net.www.protocol.http.HttpUrlConnection. If you run the same code from an applet in a browser, you'll likely get a different one, usually something that wraps the browser's native connections.

So there's no trick and no magic there, just that some classes don't show up in the API docs as they're considered internal to the implementation and subject to change.

There are plenty of similar examples, DocumentBuilderFactory or TransformerFactory are both abstract but their newInstance() method will return a subclass, usually packaged separately (e.g. Saxon).

But there are also different solutions to the same problem: JDBC defines the Driver interface (rather than an abstract class), and a DriverManager utility class with static methods to load the different driver implementations. Instead of extending an abstract class, database driver vendors are required to implement the Driver interface.

By the way, to find the actual runtime class of the object, just call getClass() on them.

like image 141
biziclop Avatar answered Oct 21 '22 09:10

biziclop


openConnection returns some concrete class which extendsURLConnection. The method's signature is defined as returning URLConnection since your code using it should not rely on any specific implementation.

like image 44
Mureinik Avatar answered Oct 21 '22 11:10

Mureinik