File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Interface issue Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Interface issue" Watch "Interface issue" New topic

Interface issue


Joined: Apr 19, 2006
Posts: 19

I know that all methods inside the interface are abstract and meant to be overriden (even though we did not explicitly declare those methods public abstract, it is automactically declared for us). Is this mean:

1. Methods inside interface have to be kept public all the time?
2. Can we declare type of the method others besides void?
3. Can we have paramateres on the methods?
4. Can we have a static methods in interface?

which of these are valid declarations in a interface definition:
1. void methoda ();
2. public double mothoda ();
3. public final double mothoda();
4. static void methoda (double d1);
5. protected void methoda (double d1);


Joined: Apr 19, 2006
Posts: 19
Because of my impatience, I have tried running all the scenarios. The result are:

1. Yes, methods have to be kept public inside interface.
2. Yes
3. Yes
4. If we try to decare a static method, we will get a response "modifier static not allowed here"

So, I assume for the 4 examples case no 1&2 are valid ones. One more point, final method is not allowed in interface because final means can not be overriden, as we know the purpose of having abstract methods are to be overriden by classes which implement the interface.

marc weber

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Erik Dirgaria:
...I have tried running all the scenarios...

That's often the best way to learn!

Interface methods are implicitly abstract and public. (These keywords can be included, but are not required.)

There are no restrictions on return or argument types. But abstract methods cannot be static. And because methods cannot be overridden with more restrictive access, interface methods must stay public.

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
I agree. Here's the link:
subject: Interface issue
It's not a secret anymore!