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

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
1
  • 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: 22783
131
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).
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic