Maxim Maxim - 5 months ago 6
Java Question

Java constructor of an abstract class

As far as I know (please correct me if I'm wrong) a class that is abstract cannot be instantiated. You can give it a constructor, but just can't call new on that class. If you call super in a subclass, the superclass constructor will run (and thus create an object of that class?) then how come you can actually call super in a subclass of an abstract class?
I'm sure it has something to do with my misunderstanding about a constructor making an object...

Answer

If you call super in a subclass, the superclass constructor will run (and thus create an object of that class??) then how come you can accually call super in a subclass of an abstract class?

This part is wrong. When you call super in the subclass constructor, you're just telling to the subclass that it first has to execute the initialization code from the super class (abstract or not), then it will continue executing the code to initialize the current instance of the class being created. This doesn't mean that it will create an instance of the super class in the middle of the creation of the current instance.

Comments