?
Solved

rules for Java Interfaces

Posted on 2010-09-11
4
Medium Priority
?
785 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 500 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses
Course of the Month13 days, 20 hours left to enroll

807 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