Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) 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.
 
Happiness is not a goal ... it's a by-product of a life well lived - Eleanor Roosevelt. Tiny ad:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!