aspose file tools*
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
Author

construtor in abstract class

Agarwal Priyanka
Greenhorn

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
Sheriff

Joined: Oct 27, 2005
Posts: 19723
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
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
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: construtor in abstract class