Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

need your help about a access qeustion.

 
Gong James
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Nisheeth Kaushal
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ravish,
Is it possible to declare someMethod with no modifier i.e. with default accessibility.
Reply soon.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Manish Hatwalne
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Manish Hatwalne
Ranch Hand
Posts: 2596
Android Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 129
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Val
You are right. I have check it out with code.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's what I though
Thanks Jose

------------------
Valentin Crettaz
Sun Certified Programmer for Java 2 Platform
 
Gong James
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi jose can you post that code?
so that everbody will know.

Thank you guys.
 
Muhammad Farooq
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Nisheeth Kaushal
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5384
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
michelle
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was saw this question at JAVA QUIZ http://www.angelfire.com/or/abhilash/Main.html
ths question was:
Question 9.

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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic