Thursday, April 2, 2009

Interface Declaration

Declaring an Interface

When you create an interface, you're defining a contract for what a class can do, without saying anything about how the class will do it. This means an interface can contain only abstract methods. Interfaces can be implemented by any class, from any inheritance tree.

Remember:-abstract class can define both abstract and non-abstract methods, an interface can have only abstract methods

Interface has following rules :

1. All interface methods are implicitly public and abstract. In other words,
you do not need to actually type the public or abstract modifiers in the
method declaration, but the method is still always public and abstract.

2. All variables defined in an interface must be public, static, and final—
in other words, interfaces can declare only constants, not instance variables.

3. Interface methods must not be static.

4. Because interface methods are abstract, they cannot be marked final,
strictfp, or native. (More on these modifiers later.)

5. An interface can extend one or more other interfaces.

6. An interface cannot extend anything but another interface.

7. An interface cannot implement another interface or class.

8. An interface must be declared with the keyword interface.

9. Interface types can be used polymorphically

Interface is a way to implement multiple inheritance.Since this feature is not available directly
so you can implement it indirectly through interfaces.Consider following

class multiple extends Ramos implements Callable{}

The following is a legal interface declaration:
public abstract interface Callable { }

both of these declarations are legal, and functionally identical:
public abstract interface Rollable { }
public interface Rollable { }

The public modifier is required if you want the interface to have public rather
than default access.


Following 5 methods declarations are legal

void boe();
public void boe();
abstract void boe();
public abstract void boe();
abstract public void boe();

The following interface method declarations won't compile:

final void boe(); // final and abstract can never be used
// together, and abstract is implied
static void boe(); // interfaces define instance methods
private void boe(); // interface methods are always public
protected void boe(); // (same as above)

Declaring Interface Constants

You need to remember one key rule for interface constants. They must always be
public static final

keyword final – prevents the value from being change.

Because interface constants are defined in an interface, they don't have to be declared as public, static, or final. They must be public, static, and final, but you don't have to actually declare them that way.

interface Fun {
int BAZ = 42;
void do();
}
class Zap implements Fun {
public void do() {
BAR = 27;
}
}

This sample code trys to change the value of BAZ which is unacceptable. Hence, the code gives compilation error.

0 comments:

About This Blog

This Blog is all about Java and programming.This blog is written by Vaibhav Pandey .He is a Computer Science Graduate .You can contact the author at javatute@gmail.com for any suggestion or Query.You can also contact the author for advertising on this blog.All the material presented here is the property of author and its reproduction in any form is strictly prohibited.

Disclaimer:-Download links provided here are not of author in any means.These are found over the Internet.
Page copy protected against web site content infringement by Copyscape

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP