Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructor

 
josephine chen
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
------------------
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic