Solved

rules for Java Interfaces

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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.

706 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

18 Experts available now in Live!

Get 1:1 Help Now