0

why??error?

class ABCD { public int i=0; public ABCD(String text) { i=1; } } class Sub extends ABCD { public Sub(String text) { i=2; } public static void main(String args[]) { Sub b=new Sub("hello"); System.out.print(b.i); } }

17th Dec 2016, 6:20 PM
Somnath Ghosh
Somnath Ghosh - avatar
2 Answers
+ 2
In class ABCD since you have defined a parametrized constructor so the compiler now won't automatically create the default constructor ABCD(){} so when you create an object of the derived class. there is no matching constructor to be called for base class. Here, there are 2 wats to get your code working: 1. add super(text); as first line inside derived class's constructor 2. define the default constructor for base class explicitly ABCD(){}
17th Dec 2016, 7:53 AM
Caffeinated Gamer YT
Caffeinated Gamer YT - avatar
0
Another reason, (Neeraj's reason it's correct too) Is because you didn't define the data types of your "i" variables
18th Dec 2016, 7:53 AM
Dago VF
Dago VF - avatar