Forums Register Login

Constructor

+Pie Number of slices to send: Send
[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]
+Pie Number of slices to send: Send
------------------
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.
All of life is a contant education - Eleanor Roosevelt. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 687 times.
Similar Threads
Mock Exam question
Constructor question
constructor help pls
JavaPrepare Q
hi i need help..please help me...
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 06:26:46.