What Are Constructor Methods?

Constructors from a conceptual level enable the creation of an object of a given class. Constructor is a method of a class with no return parameters. Constructors also enable the initialization of any variables of a class. You can supply input parameters to constructors.

A class can have more than one constructor. It has the same name as the class itself. A default constructor doesn’t have any parameters. In case of inheritance - if your subclass constructor does not explicitly call the superclass constructor, java will call the super class default constructor. If you want subclass constructor to call a specific superclass constructor, use the ‘super’ keyword.

Example:

Class Book
{
	String title;
	String publisher;
	float price;
        public Book()
        {
        }
        public Book(String title)
        {
          this.title = title;
        }
}
Class TextBook extends Book
{
	public TextBook(){
}
public TextBook(String title)
{
	super(title);
}


FAQ Home

 
Printer friendly version of the FAQ-JAVA-What-Are-Constructor-Methods page


Sponsored links

Build IT Knowledge with Current & Trusted Content
Helps Employees Develop & Hone New Technical Programming Skills. Sign Up & Get Full Access.
Check Out IT Certification Preparation Materials
Sign Up With SkillSoft & Get Access to Training Materials for Over 50 Professional Certifications.
Software Localization Tool Sisulizer
Localize DotNet, C++ Builder, Delphi, C/C++, Visual Basic & Java apps & html help. Try Sisulizer now
CSTSOFT Instrumentation .NET & ActiveX Components
A collection of 13 instrumentation .NET/ActiveX/VCL components including Gauge,Knob,LED,Trend etc.
Web based bug tracking - AdminiTrack.com
AdminiTrack offers an effective web-based bug tracking system designed for professional software development teams.

Advertisement



Free Magazine

Free Magazines
eWeek The essential technology information source for builders of e-business.... subscribe now

Newsletter | Submit Content | About | Advertising | Awards | Contact Us | Link to us |
© 1996-2008 Community Networks Ltd All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Synchron Data - .NET development.