Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is the deal with Constructor

 
zoheb hassan
Ranch Hand
Posts: 154
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21506
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Eduardo Bueno
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They're for what you're up to. A default constructor always calls it's superclass constructor.
 
Pooja Prakash
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 154
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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....
 
John de Michele
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic