wood burning stoves 2.0*
The moose likes Java in General and the fly likes Abstract Class Constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Abstract Class Constructor" Watch "Abstract Class Constructor" New topic
Author

Abstract Class Constructor

Niranjan Prasad
Ranch Hand

Joined: Aug 13, 2005
Posts: 62
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

Joined: Feb 03, 2004
Posts: 456
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

Joined: Nov 24, 2005
Posts: 14688
    
  16

And please don't post the same question into several forums. You've already asked this in the Servlet forum.


[My Blog]
All roads lead to JavaRanch
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
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.
 
wood burning stoves
 
subject: Abstract Class Constructor