• 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

Why it want super class constructor

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class X{
X(String s){}
}
class Inherit extends X{
}

If you compile above code it gave an error why?
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kish,

You need to understand that whenever a class is defined with no constructor, then only compiler will insert a default no-arg constructor to that class and calls the no-arg constructor of the super class.

So, In your case the class Inherit is not having any constructor. So compiler will insert a default no-arg constructor and calls the no-arg constructor of the super class which is X in your case. But since the class X is not having the default no-arg constructor, compiler will complain that there is no default constructor exists for the class X.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

one in a class if you define a constructor. you have to explicitly define a defualt constructor.

as you have defind a construtor in class X. which takes string parameter.

while running the program .... compiler looks for the constructor with no arguements.

hope that will help you.
 
Pranob Samani
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

one in a class if you define a constructor. you have to explicitly define a defualt constructor.

as you have defind a construtor in class X. which takes string parameter.

while running the program .... compiler looks for the constructor with no arguements.

hope that will help you.
 
Pranob Samani
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

one in a class if you define a constructor. you have to explicitly define a defualt constructor.

as you have defind a construtor in class X. which takes string parameter.

while running the program .... compiler looks for the constructor with no arguements.

hope that will help you.
 
kish kumar
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Anil what you said is true.If i instantiate either X or Inherit using default constructor it can raise the error.But i not instantiated either.Still why it gave the error?
 
Anil Pradhan
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Kish,

What is the use of a class, if we are not going to instantiate it and use its methods or fields through it's instances. Thats why compiler complains about this kind of construction problems.
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Once you write this code and give it to compiler, Compiler will do the following

But since X() is not present it will complain. Remember if you write a constructor for a class, Compiler will not insert default no arg constructor.

Here you dont have to instantiate to see the error, Its simple that a method is being called which does not have any definition defined and hence you see error. The above case is similar to
 
What are you doing in my house? Get 'em tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic