• 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:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Constructors ??

 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


In the above example why is it necessary to introduce a no-arg constructor (line 1)in the D class, failure to do so results in complier error.

pl help.
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Every constructor must have a call to super() or this() in it as the first statement. If you don't put it there the compiler will. If the super class doesn't have a no-args constructor you have to explicitly call one of the other constructors.
 
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
whenever u define any constructor , it will call its super class default construtor (ie no args construtor ).

if super class did nt have no args contructor it will cause compile error


class A{
A(int i){
// here it will implicitly call the super class no arg constructor
}

}


S.D.Balasubramani
SCJP 1.4
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its really not necessary to introduce a no-arg constructor in your super class. Just make a call to one of the constructors of the superclass from your subclass constructor or make a call to the other constructor through "this(argList...)" in the subclass that has a "super(argList...)"
 
Soumy Kumar
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks a lot..
I understood the concept ..
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i'm reopenning this discussion as i'm still not convinced here the code is


so we can see here that we get a compilation error when call the single argument cnstrtr of derived class by EBH103(4. now it goes to the corresponding cnstctr and from there will it call super() or super(x)as it finds a cnstrctr call there???
now if if it calls super() first and after execution of it comes back to its own body and then again finds super(x) there and goes to super class again then again come back......i think it doesn't work like this....it should, finding super(x) there, call single arg constrtr of the super class.....but it's like this than it means default no-arg cnstrctr is noway getting aclled here so why does it not compile because of the very same reason.......???

thanx amit
 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What first happens when a class's constructor is called? If the constructor does not specifically invoke the superclass's constructor, the JVM calls the superclass's no-argument constructor, whether that constructor is the default constructor or a no-argument constructor. (There's a difference.)

In other words, the JVM treats a constructor with no call to super() as though the first line of that constructor invokes super(). That's why you need to have a no-argument constructor or explicitly invoke another form of the superclass's constructor.

Your code snippet has:



This is where the call to super() is happening. The compiler knows that you're implicitly invoking super(), but it doesn't see a constructor with that no-arg signature, so it generates a compiler error.
Does that help?
 
Amit Das
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
sorry Jeff i didn't get ur point here ....
you say that the constructor with 2 args has a call to super() that's fine but we are not calling it from the derived class anyway...
ammmm....if i try to understand what you mean is that there might be a call to that cnstrtr and then it will throw error so compiler dosen't allow the possibility of going to that point ...it's saying that it has tobe proper irrespective of whether you call that cnstrtr or not!!!

Plz corect me if wrong................
 
Jeff Bosch
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was responding to your original post, where EB... was a subclass of D. If you don't invoke a constructor of a class's superclass, the JVM will automatically, implicitly invoke the superclass's no-arg constructor, whether you want it to or not.

Did that help?
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Amit

The code you posted has an error. Both the classes in your example are extending from Object. You are trying to call Object's constructor which takes an int. There is no cons in Object class that takes an int. Hence the program won't compile.


Code causing the error.

[ March 25, 2005: Message edited by: Anupam Sinha ]
 
Amit Das
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah...typo!!!my apologies....

to demonstrate what i said i have a code (this time its okay )



if u run this wont give any compilation error as now there is no possibility of super() getting called so okay....

thanx
amit
 
Looky! I'm being abducted by space aliens! Me and this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic