File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question from Dan's test Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question from Dan Watch "Question from Dan New topic
Author

Question from Dan's test

Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
Suppose that the compiler generates a default constructor for a class. If a compile-time error is to be avoided which of the following must be true?
a. The super class must have an accessible constructor that takes no arguments.
b. The no-argument super class constructor must not have a throws clause that includes a checked exception.
c. None of the above.
I didn't understand answer 'a' of the above questions.Can anyone explain what does answer 'a' mean ,with an example?
Thanks
Veena


SCJP1.4
"Continuous effort - not strength or intelligence - is the key to unlocking our potential."
*Winston Churchill
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Hi veena.

Since class B has no constructor, the compiler is generating a default constructor in Class B that calls the super() in class A. Compiling this program produces Hello.
Now

Compiler will say: "Ok, class A has a constructor, so I am not going to generate a default one."
"class B does not have a constructor, so I'll generate one for it calling the super()"
"but calling super() will invoke a constructor in the superclass with no parameters, and there's none, so... compiler error"
hope this helps
[ June 25, 2003: Message edited by: Andres Gonzalez ]

I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
hi Veena
i'll try to explain.
consider following code,

when u compile this code, u will get a compile time error.
the reason is-
"a Sub class's default constructor automatically calls no-arg constructor for the Super class". now, if that constructor is Private then it would not work, right?
here, every child class has to call super classes constructor as it needs to initialize the inherited members. this comes from the "constructor chaining" when we have inheritance if u know what i mean...
this implies that the Super has to have,
1. ACCESSIBLE and NO-ARG (default constructor for the Super itself) construtor.
which is essentially pointed by the 1st statement.
may b i'm not clear in explaining this to u ...
regards
maulin
Maulin Vasavada
Ranch Hand

Joined: Nov 04, 2001
Posts: 1873
btw
in my code u've to change the main(String[] s) to main(String[] args) as i already used 's' as Sub's object...
sorry about that...
regards
maulin
Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442

Since class B has no constructor, the compiler is generating a default constructor in Class B that calls the super() in class A. Compiling this program produces Hello.
Now

Compiler will say: "Ok, class A has a constructor, so I am not going to generate a default one."
"class B does not have a constructor, so I'll generate one for it calling the super()"
"but calling super() will invoke a constructor in the superclass with no parameters, and there's none, so... compiler error"
hope this helps
[ June 25, 2003: Message edited by: Andres Gonzalez ][/qb]<hr></blockquote>
Andres ,
The above code compiles even when you don't type the super class constructer explicitly.
The following code compiles & prints Hello.

This is where I got confused.Answer A says super class must have accesible constructer that takes no arguments.But,in my above code there is no constructer defined in superclass,but still it compiles fine.If super class defines constructer with arguments,then above code would give compiler error.So I felt answer should be only 'b'.Anybody please correct me if I am wrong.
Thanks Veena
I am sorry.By mistake I have posted the same thing again below.
[ June 25, 2003: Message edited by: Veena Point ]
Veena Pointi
Ranch Hand

Joined: Jun 20, 2002
Posts: 442
Originally posted by Andres Gonzalez:
Hi veena.

Since class B has no constructor, the compiler is generating a default constructor in Class B that calls the super() in class A. Compiling this program produces Hello.
Now

Compiler will say: "Ok, class A has a constructor, so I am not going to generate a default one."
"class B does not have a constructor, so I'll generate one for it calling the super()"
"but calling super() will invoke a constructor in the superclass with no parameters, and there's none, so... compiler error"
hope this helps
[ June 25, 2003: Message edited by: Andres Gonzalez ]

Andres ,
The above code compiles even when you don't type the super class constructer explicitly.
The following code compiles & prints Hello.

This is where I got confused.Answer A says super class must have accesible constructer that takes no arguments.But,in my above code there is no constructer defined in superclass,but still it compiles fine.If super class defines constructer with arguments,then above code would give compiler error.So I felt answer should be only 'b'.Anybody please correct me if I am wrong.
Thanks
Veena
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
Hi veena, sorry for confused you more
Look at this code

Think of this way: the compiler notices that you did not specify any constructor, so it will "internally" modify the code like this

Class A extends from Object. Now, let's say you add a constructor in A

The compiler will see the above code like this:

Notice that the compiler did not add a no-arg constructor in A because there's one already defined by you.
when the constructor in B is called it invokes super(), but you do not have a no-arg constructor in A, generating a compiler
error. So to fix this, 2 options :
-you must add yourself manually a no-arg constructor, because the compiler will not add it for you.

Or create (yourself) a constructor in B that passes the parameters you require.

So, if the compiler generates a default constructor for a class (with no arguments, as always), your superclass must have an accessible constructor that takes no arguments.
does that make sense?
[ June 26, 2003: Message edited by: Andres Gonzalez ]
Brian Joseph
Ranch Hand

Joined: May 16, 2003
Posts: 160
Could someone also explain the answer B? I know this is true because I've run through a couple examples where the superclass contstructor throws an exception. The subclass must also throw the exception. I can reason it out using rules, but I don't fully understand the theory. My way to remember this is that super() is implicitly called, so you have to catch or throw it. You can't catch it because super() must be the first line in your constructor and nothing can come before it. So you don't have the "room" so to speak to do

[ June 26, 2003: Message edited by: Brian Joseph ]
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Brian your reasoning is perfect to remember that the checked exceptions declared by a base constructor, also must be declared in the derived constructor (i). To this respect, they behave the other way round as the ones declared in overriding methods: You do not need to declare, in the overriding method, those checked exceptions that were declared in the overriden method.
There is another difference. An overriding method cannot declare more checked exceptions that those declared in the overriden method (ii). However a derived constructor can declare any exception regardless those declared in the base constructor .
(i) the reason is that the throwing of an exception signals the failed construction of an object. This fact should not be masked; because the object is faulty should not be used.
(ii) the reason is that an object of the derived type could be passed to a method was writen for the base type. Thus, the method only handles the exceptions that the base type can throw. If the derived type were allowed to throw more exceptions, the method would not be able to manage them. The compiler will not allow this breakage
[ June 26, 2003: Message edited by: Jose Botella ]

SCJP2. Please Indent your code using UBB Code
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question from Dan's test