• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

What is the deal with Constructor

 
Ranch Hand
Posts: 154
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23923
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 159
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
They're for what you're up to. A default constructor always calls it's superclass constructor.
 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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....
 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic