aspose file tools
The moose likes Beginning Java and the fly likes Public constructor in abstract classes Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "Public constructor in abstract classes" Watch "Public constructor in abstract classes" New topic
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
    
    3

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Public constructor in abstract classes
 
Similar Threads
Why we need constructor in Abstract Class ??
A simple basic question about instance and object
Significance of constructors in abstract class
Interface vs AbstractClass
JSF annotations on interface or abstract class