• 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

default constructor

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please see the following code
class sun
{
sun(int i)
{
System.out.println("constructor in sun(int i ) ");
}
}
class abs extends sun
{
abs(int i)
{
System.out.println("constructor in abs(int i ) ");
}
public static void main(String args[])
{
sun s = new sun(10);
abs a = new abs(10);
}}
why is it giving the following error?
abs.java:10: No constructor matching sun() found in class sun.
abs(int i)
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you compile all of these code in the same source file?
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your constructor for class abs is abs(int i). When this code is compiled, it tries to call the no-arg constructor in the super class and you don't have one.
In general If your constructor does not make call to an overloaded constructor by this(..) or to a superclass constructor by super(..), then a call is ALWAYS made to the superclass's no-arg constructor.
Vikram
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you explicitly construct a constructor for any class, the default no-args constructor ceases to exist. There is absolutely no problem as long as yur constructor is not overloaded.
In your program, you have a super class and a sub class. You have an overloaded constructor in the superclass. SO the no-args constructor ceases to exist. When you create an instance of the sub-class, the constructor call is down the hierarchy, (ie) super to sub class.
The compiler always looks for the no-args constructor in the super class unless you have EXPLICITLY told the compiler which constructor of the super class it shud call.
Try calling the corresponding super class constructor in the sub class const. using super(args) and yu will have no problem at all. Else give a no-args constructor to the super class and the code will compile.
To put it short, when instantiating a sub class object, the constructors are called from super class downwards and unless you make a call to a particular cons. of the super class, by default the compiler runs to the no-args constructor of super class.
Hope that helped.
--Vasanth
 
Sunita Vontel
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You everybody
That helped me a lot
 
Greenhorn
Posts: 21
Eclipse IDE Python Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry ..guys I am still confused about the given problem !
Vasanth you said that explicit call to the super class constructor should be made in a case where it is overloaded (as the case here)...but aren't we doing that by the following code fragment.. " sun s = new sun(10);" .. shouldn't call the appropriate contructor of the super class ???
Is it that we cannot call overloaded contructors of super class like this ?? ..and that we have to do it using "super" keyword ???
rgds
Manoj
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Manoj,
In this example, we have a Base class called sun from which the abs class is derived. Now when we instantiate the abs class using
abs a = new abs(10);
the default constructor or the no-args constructor of the base class is called since you have not made an explicit call to any of the base class constructors using super.Since you have defined a constructor in the class called sun, the default constructor does not exist. So you need to explicitly define the no args constructor.
so adding the following line
sun(){} will remove the compile time error.
Hope this helps.
 
The first person to drink cow's milk. That started off as a dare from this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic