aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Constructor 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 "Constructor" Watch "Constructor" New topic
Author

Constructor

josephine chen
Ranch Hand

Joined: Oct 29, 2002
Posts: 216
[code]
class create{
create(int j, int k){}
public static void main (String s[]){
Test t = new Test(1,2);
}
}

class Test extends create{
public Test(int j){}/*if i comment this line it works perfectly why??*/
public Test(int j,int k){super(j,k); }
}


[\code]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
------------------
class Test extends create{
public Test(int j){}/*if i comment this line it works perfectly why??*/
public Test(int j,int k){super(j,k); }
}
---------------------
When you declare public Test(int j){}
Compiler calls a no arg default constructor (super()) implicitly. As you do not have a default no arg constructor in create class the compilation fails.
You should explicitly create a no arg default constructor in create class or explicitly call super( j, k) in the first line of your code. public Test(int j){super(j,0);}/*
Note that if you do not create your own customized construtor Java creates a default no arg constructor for you, but if you create one then the default no arg constructor is not created, so, create class do not have default no arg constructor.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Constructor