Solved

rules for Java Interfaces

Posted on 2010-09-11
4
769 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
word0 challenge 3 77
HashMap Vs TreeMap 12 55
what is the difference between "sudo su" and "su - root" 6 101
bitbucket vs gitbucket 3 55
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 connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

943 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now