• 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
  • 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:
  • Quote
  • Report post to moderator
Why the code doesn't get compiled? Can anyone explain me.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are creating a sub class by using superclass.
Inheritance rule is that

The superclass should have a default constructor then only we can create a sub class for it.

So now compile this.
class Y{
Y(){}
Y(String s){}

}
class Z extends Y{
Z(){}
}
 
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:
  • Quote
  • Report post to moderator
Hi Madhu,
Why should i need to declare a default constructor in the super, if there exist a implicit default constructor declaration in the super.
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Why should i need to declare a default constructor in the super, if there exist a implicit default constructor declaration in the super.



No when you declare your own constructor in any class , the compiler no longer provide its own default constructor !!

If thats clear,

Now see this code :



when you call the constructor of sub-class , the compiler execute the firs line as the call to super class constructor ..

in your original code , you are not providing the default constructor like this :


and when compiler sees this code :


it replace first line of the subclass constructor like this :


so there is no default constructor for super class , hence compiler raise an error !!

Hope this help !
 
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:
  • Quote
  • Report post to moderator
Thanks a lot Sagar. I understood the stuff hence corrected myself.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sagar Rohankar:
No when you declare your own constructor in any class , the compiler no longer provide its own default constructor !!


Ding ding ding! We have a winner!

The compiler only inserts a default constructor if you don't provide any constructors at all. And all that constructor does is call its parent's class' constructor without parameters, like so:
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic