File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Construtor Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Construtor Doubt" Watch "Construtor Doubt" New topic
Author

Construtor Doubt

Karu Raj
Ranch Hand

Joined: Aug 31, 2005
Posts: 479
When I compile the following code ? I got the error as

---------- Javac ----------
Test2.java:14: Sup(java.lang.String) in Sup cannot be applied to ()
public Test2(String str){
^
----------------------------------------------------

Please do explain me why???

class Sup{

public Sup(String str){

System.out.println("Super class");

}

}


public class Test2 extends Sup{

public Test2(String str){

System.out.println("Sub class");

}

public static void main(String[] args) {

Test2 t2 = new Test2("22");

}

}
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Constructor must have *super* or *this* at the first line.

In your case, you haven't provide any:



So compiler will insert *super* like this:


But since you yourself have typed a *argument constructor* in your super class, so complier will not insert default one, but in sub class compiler is trying to call that....

And thats why it is giving error...



[ December 13, 2005: Message edited by: rathi ji ]
Purujit Saha
Ranch Hand

Joined: Nov 01, 2005
Posts: 86

Constructor must have *super* or *this* at the first line.

In your case, you haven't provide any:


code:
--------------------------------------------------------------------------------

public Test2(String str){ System.out.println("Sub class"); }



I don't thing that was the reason behind the compiler error.
if we do not implicitly write super() or this() as the first argument in a constructor then the compiler itself will insert & call the super().

So, my friend Karthik Rajashekaran , the main problem is you have written a constructor in your class, so the compiler will not write any default constructor in your class,
so,

class Sup
{

public Sup(int i)
{
System.out.println("Super class");

}

}


public class Test2 extends Sup
{
public Test2(String str)
{
//super(); this will be inserted by compiler
System.out.println("Sub class");

}

public static void main(String[] args)
{
Test2 t2 = new Test2("22");

}

}

will give error since there is no default constructor in super class Sup.

-------------------------------------------------------------------------
You also can test this,

public class Test2
{
public Test2(String str)
{
System.out.println("Sub class");

}

public static void main(String[] args)
{
Test2 t2 = new Test2();

}

}


this will also give you the same error.
[ December 13, 2005: Message edited by: Purujit Saha ]
tim bond
Greenhorn

Joined: Nov 21, 2005
Posts: 18
class Sup{

public Sup(String str){

System.out.println("Super class");

}

}


public class Test2 extends Sup{

public Test2(String str){

System.out.println("Sub class");

}

public static void main(String[] args) {

Test2 t2 = new Test2("22");

}

}



the solution is to insert


super(str);



in class Test2

public class Test2 extends Sup{

public Test2(String str){

super(str); // insert the super call with the argument.

System.out.println("Sub class");

}

public static void main(String[] args) {

Test2 t2 = new Test2("22");

}

}


This will resolve the issue.
Sasikanth Malladi
Ranch Hand

Joined: Nov 04, 2000
Posts: 175
I had only 3 hrs sleep last night so I guess I'm a little slow but, Purujit, you seem to say the exact same thing that Rathi did.
Sashi
Lakshmanan Arunachalam
Ranch Hand

Joined: Nov 02, 2005
Posts: 99
I will suggest to add default no-arg constructor in a class, at times when you define some arg constructor in the same class and you believe the class may be extended by other classes. Because, what the compiler will do is, first it check for if the class has any constructor(args or no-args), if it finds one, then it wont provide default no-args constructor.

As you have done, if you extend a class which
a. doesn't have no-arg constructor
and
b. has one or more constructors with arguments,

the compiler will check for call to super class constructor, if there is no call for super class args constructor explicitly, then as said compiler will add call to default super class constructor, search for default constructor, since there is no default consttructor in the super class, it will throw compile time err.


Regards<br />Lakshmanan<br />IBM-OOAD & UML, SCEA-I
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Construtor Doubt
 
Similar Threads
Is it must to call super class constructor?
Thread synchronization doubt
Doubt in Constructors
constructor giving headache
Same doubt again regarding constructors