Solved

rules for Java Interfaces

Posted on 2010-09-11
4
774 Views
Last Modified: 2012-08-14
I have compiled the following rules. Can anyone finish this for me:
Interface is a multiply implementable class contract of sorts
all methods are implicity public and abstract
all interface methods must not be static
because interface methods cannot be marked final,strictfp,transient or sychronized or native
within the implementing class synchronized
the implemented method can be marked synchronized though
All variables in an interface are public, static and final
Its only meant to hold constants not variables that change.
Interfaces are defined one per file because its public
abstract methods can not be marked final and all methods defined in an interface are abstract so no final methods in an interface definition.
Interfaces like classes can extend  but only interfaces
classes likewise can only extend other classes  including abstract ones.
classes can only implement interfaces but many of them
e.g class tire extends RubberClass implements Bounceable, Inflateable,serializeable
extending only works one class or interface at a time.

I recently found that modifiers such as synchronized are not allowed within an interface definition but in fact are allowed within the implementation
e.g interface  Booable {
          final static int myvar1=123;
         void doblue();
}

it can not be
 interface  Booable {
          final static int myvar1=123;
         synchronized void doblue();
}
but with the former interface
i could create a class
class Itest {
        public synchronized void doblue()
        {}
        public static void main(String [] args)
        {
        //      myvar1=45;
        int xyz=0;
        new U2();
        System.out.println("testing " + myvar1);
        }

}

why is that? What about all the other modifiers or is synchronized the only exception???
0
Comment
Question by:Robert Silver
  • 2
  • 2
4 Comments
 
LVL 2

Accepted Solution

by:
tisaksen earned 125 total points
ID: 33654444
http://java.dzone.com/tips/java-interface-rules

synchronized is an implementation detail  (thread controll), so naturally it isn't allowed in an interface. An interface should not contain "implementation" details such as wether the method should be synchronized or not.
0
 
LVL 2

Author Comment

by:Robert Silver
ID: 33677542
Okay but then  what about static methods? Can static methods be interface methods?
what about all the other modifiers???

0
 
LVL 2

Expert Comment

by:tisaksen
ID: 33679182
No you can't have static methods in an Interface. All method declarations must be public. If you don't explicitly define it as public it will be implicitly public.

You should read this tutorial which will answer most of your questions about interfaces:
http://download.oracle.com/javase/tutorial/java/IandI/createinterface.html
0
 
LVL 2

Author Closing Comment

by:Robert Silver
ID: 33791706
That was the explanation I was looking for
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Maven Project: Hibernate Dependencies Conflict 10 54
java imports not found 4 52
Java syntax, or is it Selenium 6 43
Tagging and Merging on Branch 1 45
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

697 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question