File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Can abstract class have constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Can abstract class have constructor" Watch "Can abstract class have constructor" New topic
Author

Can abstract class have constructor

Hemal Mehta
Ranch Hand

Joined: Nov 16, 2000
Posts: 101
If so why?
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Sure they can...
you cannot instantiate an abstract class but when you subclass it and you create an instance of your concrete subclass you may want to initialize things in your abstract superclass. This is why constructors in abstract classes may be useful...
Remember that when you create an instance of an object, all classes in the hierarchy tree have to be initialized too and their constructors will be invoked. So even if the class is abstract his constructor(s) will be invoked...
[ February 26, 2002: Message edited by: Valentin Crettaz ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Brian Lugo
Ranch Hand

Joined: Nov 10, 2000
Posts: 165
You can declare a class as a abstract even if it does not have a single abstract method, i.e. you can declare a fully implemented class as abstract.
Brian
Rajinder Yadav
Ranch Hand

Joined: Jan 18, 2002
Posts: 178
Here is some code that shows what Valetin said

[ February 26, 2002: Message edited by: Rajinder Yadav ]

<a href="http://www.rajindery.com" target="_blank" rel="nofollow">Rajinder Yadav</a><p>Each problem that I solved became a rule which served afterwards to solve other problems. --Rene Descartes
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
Thanks Rajinder,
I was gonna write some code but I had to take off, so you spared me some work... How kind of you
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can abstract class have constructor