If you declare a constructor, the compiler does not declare a no-arg constructor. So, in Q6, A will fail due to the absence of a no-arg constructor. In Q7, if public Test(int j) is invoked then the superclass's no-arg constructor will be invoked. So, either you must declare a no-arg constructor in Base or invoke a superclass's arg constructor from Test. Just remember that for every constructor in a subclass which does not call super, the compiler will expect to see a no-arg constructor in the superclass. Incidentally, I invariably declare a constructor in my programs, it makes life much easier. [ April 15, 2003: Message edited by: Roger Chung-Wee ]
default constructor is one with no argument. So where is Test()? NO. Therefore new Test() is wrong. Q7: What is the code for Base class? Without it we cannot judge Base() is correct or not.
Joined: Mar 12, 2003
" In Q7, if public Test(int j) is invoked then the superclass's no-arg constructor will be invoked. So, either you must declare a no-arg constructor in Base or invoke a superclass's arg constructor from Test."
Q6 says that B is right... According to your above statement, If B is right then it it will invoke the the superclass's no-arg constructor ... So if they are correctly compiled, we assume that Test() exists and we shud be able to call option A: new Test() for Q6 I feel that without the Base class definition in Q6 there is no real way to determine correctness of the answers Am I right in making these assumptions? Can anyone give me concrete explanations as to why the answers are what they are
Q7: What is the code for Base class? Without it we cannot judge Base() is correct or not.
We don't need Base class code to decide this. Think about it this way. Base class will have a non argument constructor because of line numbered 5. Right?? This implies that compiler will not provide implicit default constructor when reqd. One has to explicitly provide it. Now, what will the constructor Test(int j) call when it does not have an explicit call to base class constructor?? Definitely! default constructor. So,compiler will complain if we don't provide Base class' default constructor explicitly. Hope, i made myself clear.