File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes construtor in abstract class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "construtor in abstract class" Watch "construtor in abstract class" New topic

construtor in abstract class

Agarwal Priyanka

Joined: Jul 25, 2008
Posts: 20
Can there be a construtor in an abstract class, anyhow?

keep trying...thats the way to success...<br />with best regards...<br />p.agarwal
Rob Spoor

Joined: Oct 27, 2005
Posts: 20273

Have you tried it? I guess not, otherwise you would have found out it is possible.

Of course this makes sense - an abstract class is still part of a class hierarchy, so constructor chaining will be the the same as with non-abstract classes.

How To Ask Questions How To Answer Questions
Mark Vedder
Ranch Hand

Joined: Dec 17, 2003
Posts: 624

This can be a bit confusing. You can't construct an abstract class directly. But it can have a constructor. This is so, as Rob points out, its constructor gets called either implicitly via constructor chaining or explicitly (via the super keyword) to allow for it to be constructed (and have values initialized or what have you). Take Rob's suggestion and try some examples.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46360
Of course you need a constructor for an abstract class. I sometime quote an example like this:where the Vehicle class is abstract. But all vehicles have common characteristics.Now, the way I look on subclasses is that they incorporate a part from the superclass, which I am sure is not quite accurate.
A Car or a Bus is a Vehicle, and they have number plates (only they are called something different in different countries), so when you write the constructor for a Car it would include "super(numberPlate);" That means you are instantiating the "Vehicle" part. Then you can instantiate the "Car" part, in the rest of the constructor.

In fact every class has a constructor; if you don't write one the compiler will add a default constructor for you.
I agree. Here's the link:
subject: construtor in abstract class
It's not a secret anymore!