Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Java Abstract Class Constructors

Status
Not open for further replies.

huttemannw

Programmer
Feb 6, 2003
12
0
0
US
Hi,

I'm still learning java and have a question about abstract classes:

If an abstract class cannot be instantiated, why does it have a constructor(s)?

Take, for example, the Writer class from the java.io package. It has two.

Thanks for any help.
 
Because it forces any extending classes (such as classes that you can instantiate) to use the defined contstructors in the abstract class.

Its a way of ensuring full implementations of a framework such as the java.io package are compatible, and standard.

--------------------------------------------------
Free Database Connection Pooling Software
 
Thanks for the response, but I still don't follow (I'm thickheaded sometimes). The concrete classes that extend the abstract class have their own constructors. How can they implement the abstract class' constructor?
 
They don't implement it.

If you extend a class, your constructor has to call the base-constructor.

Often you don't write an explicit constructor, so javac will generate a default-constructor (without arguments), and call the baseconstructor implicitly - which will only work, if the baseconstructor doesn't expect an argument too - which is often possible, - i.e.: the baseclass has only a default-constructor too.

seeking a job as java-programmer in Berlin:
 
OK, lets do away with the word "implement" because that has too many connotations with interfaces.

The point of a abtract class is to provide some base fuctionality, but be loose enough so that they can create their own methods. So you can have constructors that are not defined in the abstract class, but the signature of certain constructors is enforced.

Perhaps you should take the time to read some documetation :
Always a good starting point.

--------------------------------------------------
Free Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top