What is the requirement of the class which implements the following Interface 1. public interface Test { 2. void someMethod(); 3. } Select all correct answers a. Should have someMethod which must be necessarily declared as public. b. Should have someMethod which could be "friendly" or public c. Should have someMethod which should not throw any checked exceptions. d. Should have someMethod which cannot be sychronized as sychronized is not in the signature of the interface definition.
R K Singh
Ranch Hand
Joined: Oct 15, 2001
Posts: 5369
posted
0
a. Should have someMethod which must be necessarily declared as public. correct as methods from interfaces are public by default. b. Should have someMethod which could be "friendly" or public incorrect/wrong as friendly is not keyword of java. c. Should have someMethod which should not throw any checked exceptions. incorrect/wrong while implementing a method , one should follow all the rules of overriding d. Should have someMethod which cannot be sychronized as sychronized is not in the signature of the interface definition. incorrect/wrong No, implemented method can be synchronized or native or strictfp.
------------------ Regards Ravish
"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
Nisheeth Kaushal
Ranch Hand
Joined: Jul 20, 2001
Posts: 87
posted
0
Hi Ravish, Is it possible to declare someMethod with no modifier i.e. with default accessibility. Reply soon.
Valentin Crettaz
Gold Digger
Sheriff
Joined: Aug 26, 2001
Posts: 7610
posted
0
No you can't since methods in interface are implicitely public (and abstract) so you can't declare someMethod with default accessibility, you have to declare it public ! HIH ------------------ Valentin Crettaz Sun Certified Programmer for Java 2 Platform
Originally posted by Nisheeth Kaushal: Is it possible to declare someMethod with no modifier i.e. with default accessibility.
It is possible to declare someMethod() in an interface with no modifier, but it will still be public, as all the methods in the interface are by default public. HTH, - Manish
Originally posted by Valentin Crettaz: No you can't since methods in interface are implicitely public (and abstract) so you can't declare someMethod with default accessibility, you have to declare it public !
Valentin, I agree all the methods in an interface are by default public & abstract, but I believe you can have a method in an interface w/o an access modifier, of course, it'll still be public. Pls. correct me if I am wrong. - Manish
[This message has been edited by Manish Hatwalne (edited October 22, 2001).]
Valentin Crettaz
Gold Digger
Sheriff
Joined: Aug 26, 2001
Posts: 7610
posted
0
yep right, you can declare it in the interface with no access modifier but public is implied. You can't declare it without access modifier in your class implementing the interface, though !
------------------ Valentin Crettaz Sun Certified Programmer for Java 2 Platform
Darryl Failla
Ranch Hand
Joined: Oct 16, 2001
Posts: 127
posted
0
Why is C wrong? My understanding is that any overriding method can only throw exceptions that are a subset of the exceptions thrown by the overridden method. In this case the overridden method throws none.
Darryl Failla
Sun Certified Java 2 Programmer
Valentin Crettaz
Gold Digger
Sheriff
Joined: Aug 26, 2001
Posts: 7610
posted
0
Yeah I agree with you Darryl. Since the method in the interface doesn't have any throw clause which means no checked exception are thrown, then the method implementing someMethod cannot thrown any checked exception. d is true though since synchronized is not part of the signature of a method. The signature of the method in an interface cannot be declared synchronized but it is perfectly legal to implement a method in a class and declared it synchronized since synchronization is an implementation issue. To sum up, a,c and d are correct ! Am I right there ?? ------------------ Valentin Crettaz Sun Certified Programmer for Java 2 Platform [This message has been edited by Valentin Crettaz (edited October 22, 2001).]
Jose Botella
Ranch Hand
Joined: Jul 03, 2001
Posts: 2120
posted
0
Hi Val You are right. I have check it out with code.
SCJP2. Please Indent your code using UBB Code
Valentin Crettaz
Gold Digger
Sheriff
Joined: Aug 26, 2001
Posts: 7610
posted
0
That's what I though Thanks Jose
------------------ Valentin Crettaz Sun Certified Programmer for Java 2 Platform
Gong James
Ranch Hand
Joined: Oct 15, 2001
Posts: 77
posted
0
What is the requirement of the class which implements the following Interface public interface Test { void someMethod(); } Select all correct answers a. Should have someMethod which must be necessarily declared as public. b. Should have someMethod which could be "friendly" or public c. Should have someMethod which should not throw any checked exceptions. d. Should have someMethod which cannot be sychronized as sychronized is not in the signature of the interface definition.
THE GIVEN ANS :bd MY ANS :c if the given option changed to following ,I think all of following options are correct.somebody agree with me.if not ,pls correct me. a) may have someMethod which declared as public . b) may have someMethod which declared as "friend" or public . c) may have somemethod which mustn't throw any checked exceptions. c) may have somemethod which cann't be sychronized as sychornized si not in the signature of the interface definition.
suma krishnan
Ranch Hand
Joined: Oct 19, 2001
Posts: 42
posted
0
Hi jose can you post that code? so that everbody will know.
Thank you guys.
Muhammad Farooq
Ranch Hand
Joined: May 08, 2001
Posts: 356
posted
0
Thanks Val for ur reply, I think d is wrong d. Should have someMethod which cannot be sychronized as sychronized is not in the signature of the interface definition. as you have also explained iy: d is true though since synchronized is not part of the signature of a method. The signature of the method in an interface cannot be declared synchronized but it is perfectly legal to implement a method in a class and declared it synchronized since synchronization is an implementation issue. To sum up, a,c and d are correct ! Please comment?? --Farooq
Muhammad Farooq<br />Sun Certified Programmer for Java 2 Platform<br />Oracle8i Certified Professional Database Administrator
Nisheeth Kaushal
Ranch Hand
Joined: Jul 20, 2001
Posts: 87
posted
0
Hi Farooq, Valentin is right d is also true. But is it possible that an interface need explicitly to throw an checked exception. And since the interfacing is more of similar to abstraction, the method in the interface doesn't have any throw clause which means no checked exception are thrown, then the method implementing someMethod cannot thrown any checked exception. Which means c is false. Correct MIIAW Nisheeth.
R K Singh
Ranch Hand
Joined: Oct 15, 2001
Posts: 5369
posted
0
Originally posted by Nisheeth Kaushal: Hi Ravish, Is it possible to declare someMethod with no modifier i.e. with default accessibility. Reply soon.
Sorry guys i miss this discussion coz of work... Kaushal U have got ur answer... and yes I am wrong... 'c' is true too. I think, when we are not throwing explicitly any exception then by default we are throwing "Exception", as exception can be thrown in a method anytime(bcoz of any problem). and that's why overriden method can throw any checked exception even if super.aMethod() is not throwing any. Though i said that it follows all the rule of overriding while implementing method(I am human, i myself forgot... ) CMIW ------------------ Regards Ravish
public interface AQuestion { void someMethod(); } The class which implements AQuestion a.Should have someMethod which must necessarily be public. b.Should have someMethod which could be "friendly" or public c.Should have someMethod which should not throw any checked exceptions. d.Should have someMethod which cannot be sychronized as sychronized is not in the signature of the interface defination I agree their answer : a c Pallie SCJP
Michelle<br />SCJP<br />OCP Developer 2, 6/6i
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.