| Author |
What is the deal with Constructor
|
zoheb hassan
Ranch Hand
Joined: Apr 01, 2009
Posts: 146
|
|
|
like i said what is the deal with them,Some books say they are for initialization of instance variables,Some say they are for calling their super class constructor,Can anyone please explain to me in detail about these Constructors
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16681
|
|
zoheb hassan wrote:like i said what is the deal with them,Some books say they are for initialization of instance variables,Some say they are for calling their super class constructor,Can anyone please explain to me in detail about these Constructors
The purpose of a constructor -- is used to initialize (ie. construct) the instance of a class. If your instance needs to have variables initialized, then you can do it in the constructor. If you extend a super class, and need to call a particular constructor of the super class, then you have to do it in the constructor. Etc. etc. etc.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Eduardo Bueno
Ranch Hand
Joined: Jun 04, 2009
Posts: 154
|
|
|
They're for what you're up to. A default constructor always calls it's superclass constructor.
|
 |
Pooja Prakash
Greenhorn
Joined: Jul 01, 2009
Posts: 9
|
|
Hi,
I'll give an example. Just think that you wanna buy a car .Here Car is a class. When you say
Car a= new Car();
the car class is instantiated.(ie you've bought the car)
Now you have specified before buying the car, that you need A/c, diesel as fuel, the colour of car, etc. All these attributes are mentioned in your 0 argument constructor. If you don't give all these details, then the store gives oyu a car with default settings.
It's the same with the constructor. If you define your own constructor, It is called when you say new Car().Otherwise, if you don't define your own constructor, you get a car with default values .(created by default constructor).
I hope you understood the concept.
-Cheers,
Pooja
|
 |
zoheb hassan
Ranch Hand
Joined: Apr 01, 2009
Posts: 146
|
|
|
Thank You Pooja and Henry,But edwardo why exactly does a constructor have to make a call to the Superclass Constructor,I wanna know this cause java does everything for a reason and i would like to know it, and also isn't the "new" keyword sufficient to create an object why follow it with a Constructor
|
 |
Ashok Suthar
Ranch Hand
Joined: Apr 05, 2007
Posts: 30
|
|
Hi,
New followed by "type" instructs to create the object of that "type".
Car c = new Car();
If you dont specify "Car()"....what object you are trying to create? How Java would know?
And how Polymorphism will work....
|
SCJP 5 (98%)...SCWCD On
|
 |
John de Michele
Rancher
Joined: Mar 09, 2009
Posts: 600
|
|
Thank You Pooja and Henry,But edwardo why exactly does a constructor have to make a call to the Superclass Constructor,I wanna know this cause java does everything for a reason and i would like to know it, and also isn't the "new" keyword sufficient to create an object why follow it with a Constructor
The reason a constructor makes a call to its superclass constructor is because an object of the subclass is also an object of the superclass, all the way up to the Object class. So, if you have a superclass A, and a subclass B, B is an A, and you can put an object of B where an A is expected, although it will only behave like an A.
The reason why the new keyword is not sufficient is that a class can have more than one constructor, and not every class has a default constructor.
John.
|
 |
 |
|
|
subject: What is the deal with Constructor
|
|
|