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)
Ada Wang
Greenhorn
Joined: Aug 28, 2000
Posts: 23
posted
0
Did you compile all of these code in the same source file?
Vikram Ranade
Greenhorn
Joined: Aug 10, 2000
Posts: 16
posted
0
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
Vasanth Appaji
Greenhorn
Joined: Aug 22, 2000
Posts: 13
posted
0
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
Joined: Aug 28, 2000
Posts: 72
posted
0
Thank You everybody That helped me a lot
Enamul Haque
Greenhorn
Joined: Jun 07, 2003
Posts: 21
posted
0
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
<b><i>We all are the components of a huge program...... the programmer is always debugging us with His debugger.</b></i>
Bala Murthy
Greenhorn
Joined: Feb 05, 2002
Posts: 4
posted
0
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.