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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Java Constructors

 
Ranch Hand
Posts: 137
Hibernate Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Why the code doesn't get compiled? Can anyone explain me.
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Please choose an appropriate forum. This is in no way an Advanced question.
 
Ranch Hand
Posts: 192
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Class Z doesn't define a constructor that takes a String, like it's parent class. Nor does it call the parent's constructor explicitly, with super("someString") from it's no args constructor.

The 2 examples below show what I mean.



or


The long story is that, before an object is constructed, it's parent must be constructed. This will go on and on all the way up to Object, everybody's daddy.
This happens either automatically or explicitly, automatically if you supply a matching constructor and explicitly if you don't, as the 2 examples show. You constructor can also call another one within the same class which in turn matches one in the parent. You'll have to play around with this a little.
 
Sidharth Pallai
Ranch Hand
Posts: 137
Hibernate Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Thanks Taariq,

You are absolutely correct,as per you, it would get compile, But i've some doubt .
Other way if i declare a default constructor in the super class ie


Then my code will compile.But why should i need to declare a default, if every class has its own default constructor.
 
Sidharth Pallai
Ranch Hand
Posts: 137
Hibernate Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Ranchers i was wrong. When we declare any constructor in a class, we replace the default constructor, which was a confusion for me until i got to know from a rancher.Hence thank you all.
 
Sheriff
Posts: 22684
128
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Report post to moderator
Please CarefullyChooseOnForum and UseOneThreadPerQuestion. This is the same as this thread (which I have just moved to the beginner's forum).
 
Can you hear that? That's my theme music. I don't know where it comes from. Check under this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
    Bookmark Topic Watch Topic
  • New Topic