File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Abstract Class Constructor

 
Niranjan Prasad
Ranch Hand
Posts: 62
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Abstract Class cannot be instantiated,then y abstract classes are
provided with constructors?
For example GenericServlet and HttpServlet have constructors?

Plse answer this,this is an interview question
 
Jan Groth
Ranch Hand
Posts: 456
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
think about subclasses of abstract clases. the abstract class might want to gurantee some initialisation process, or even some constructor functionality.

"constructor" chaining would be the keyword.

but i'd say that only protected abstract constructors make sense... :-)

many greetings,
jan
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And please don't post the same question into several forums. You've already asked this in the Servlet forum.
 
Ken Blair
Ranch Hand
Posts: 1078
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even if an abstract class cannot be directly instantiated it must still be initialized and have a constructor executed when a subclass is instantiated. This is precisely why any class without a visible constructor cannot be subclassed. In most cases this translates to "a class without a non-private constructor cannot be subclassed", however there's an exception and that is when the subclass is in the same compilation unit. Regardless, the point is that to create an instance of a class, whether it's abstract or not, it must be initialized and a constructor must be executed.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic