File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interface

 
Vivek Shrivastava
Ranch Hand
Posts: 277
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi,
I have a Little confusion. First feel free to correct me.

I think all the methods declare in a interface are public and abstract even the word �public� and �abstract� are not there.

Am I correct?
Because the answer given for the following question is 1, 3

public interface AQuestion
{
void someMethod();
}
The class which implements AQuestion
1. Should have someMethod which must necessarily be public.
2. Should have someMethod which could be "friendly" or public
3. Should have someMethod which should not throw any checked exceptions.
4. Should have someMethod which cannot be sychronized as sychronized is not in the signature of the interface defination
waiting for your reply.
vivek
 
Chris Cleverley
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct. That's why 1) and 3) are correct.
Since the method is implicitly public so the class that implements this interface should necessarily have the public access modifier for this method else it'd be more restrictive than the original method in the interface. Hence 1) and 3) is obvious.
Chris
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
Hi,
the modifiers final and synchronized can also be used in implementing methods so 4 is right too!!.

interface AQuestion
{
public void someMethod();
}
public class demo implements AQuestion
{ synchronized final void someMethod(){
}
}
Regds.
Rahul.
[This message has been edited by rahul_mkar (edited July 26, 2000).]
 
Chris Cleverley
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do not forget to add public before the method in the implementing class as Rahul has done.
Chris
 
Deepak M
Ranch Hand
Posts: 124
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by rahul_mkar:
Hi,
the modifiers final and synchronized can also be used in implementing methods so 4 is right too!!.

interface AQuestion
{
public void someMethod();
}
public class demo implements AQuestion
{ synchronized final void someMethod(){
}
}
Regds.
Rahul.
[This message has been edited by rahul_mkar (edited July 26, 2000).]

4 is not right BOSS ! u said it !
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If we read the original answer 4 carefully, it says that someMethod cannot be synchronized. Rahul's code shows that someMethod() can be synchronized. Therefore 4 is incorrect, right?
 
Anonymous
Ranch Hand
Posts: 18944
  • 0
  • send pies
  • Quote
  • Report post to moderator
Hi Deepak and Jim,
Both of you are right. One does have to read questions and answers very very carefully. I think in the test one does have sufficient time to do this.
Regds.
Rahul
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic