Forums Register Login

What is the deal with Constructor

+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
They're for what you're up to. A default constructor always calls it's superclass constructor.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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....
+Pie Number of slices to send: Send
 

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.
Tongue wrestling. It's not what you think. And here, take this tiny ad. You'll need it.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1170 times.
Similar Threads
MATH and null bug me.....
Treemap key sort
inheritance
About constructor of anonymous class!
Working on a Sample Program
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 19, 2024 04:30:23.