| Author |
Public constructor in abstract classes
|
Kalyan Anand
Ranch Hand
Joined: Feb 07, 2007
Posts: 194
|
|
AFAIK There is no way to create an instance of abstract class. Then why did the Java implementors allow providing a public constructor in an abstract class ?
|
 |
Carl Wauters
Greenhorn
Joined: Aug 02, 2007
Posts: 19
|
|
|
The reason why you still can write constructors in an abstract class: think about inheritance. A subclass constructor always calls the default constructor of the superclass, even when that superclass in abstract.(unless you explicitly call another one)
|
SCJP 5.0, SCWCD 1.4, SCBCD 5.0, SCJD, SCDJWS 5.0, SCEA/OCMJEA 5
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12929
|
|
Yes Carl, but that doesn't solve the question. If the constructor of the abstract superclass is protected, the concrete subclass can also access it. Indeed, it doesn't make much sense to have public constructors in an abstract class. You can't use them directly, since you can't create an instance of an abstract class, and such a constructor doesn't need to be public if a subclass needs to access it. Also, a subclass does not always access the no-args constructor (what you call 'default constructor'). In the constructor of a subclass you can explicitly invoke a different constructor of the superclass by using the super(...) syntax:
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
 |
|
|
subject: Public constructor in abstract classes
|
|
|