Solved

rules for Java Interfaces

Posted on 2010-09-11
4
777 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

724 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