Say you have an abstract class
...
abstract class Vehicle {
abstract public void move();
}
Without ever making a class Car extends Vehicle
or class Boat extends Vehicle
you can do this with the magic of Anonymous Classes. (Which may be why the javadoc does not say there is a subclass because it is anonymous and therefore cannot be referenced by the public api, which the javadoc represents.)
class Example {
public Vehicle foo() {
Vehicle car = new Vehicle {
@Override
public void move() {
// do some car movement, i.e. driving
}
}
return car;
}
public Vehicle bar() {
Vehicle boat = new Vehicle {
@Override
public void move() {
// do some boat movement, i.e. boating
}
}
return boat;
}
}
So no you can't instantiate an abstract class
... but that's not really the whole truth, you can do it as an Anonymous Class and implement the methods on the spot. This technically is not instantiating an abstract class
though, but it's what gets done a lot when you see this sort of thing.
Again, say you have an abstract class
...
abstract class Vehicle {
abstract public void move();
}
If the inner class
was private
then only the class that is encasing it could instantiate it. (Which may be why the javadoc does not say there is a subclass because it is private and therefore hidden from the public api, which the javadoc represents.)
class Example {
private class Car extends Vehicle {
@Override
public void move() {
// do some car movement, i.e. driving
}
}
private class Boat extends Vehicle {
@Override
public void move() {
// do some boat movement, i.e. boating
}
}
public Vehicle foo() {
return new Car();
}
public Vehicle bar() {
return new Boat();
}
}
This doesn't explain anything about instantiating abstract class
es, but it may help answer about URL
, openConnection
, and URLConnection
.