What is an interface?

September 13th, 2007 by uCertify Leave a reply »

An interface is a reference type that defines a contract. An interface body consists of method declarations and constants. All methods and constants in an interface are public. Interfaces are left completely unimplemented, i.e., no method in the interface is implemented. All methods of an interface are abstract and the method body is absent. A class may access one or more interfaces simultaneously. An interface is defined using the syntax below:

access_modifier interface interface_name extends Super_interfaces{
return_type method1(parameter_list);
.
.
.
return_type methodn(parameter_list);
datatype variable1 = value;
.
.
.
datatype variablen = value;
}

The syntax defined above has two components: the interface declaration and the body of the interface. They are discussed in the following sections:

An interface declaration

An interface declaration consists of the following components:

  • access_modifier: An interface can be declared by using the public access modifier or without any access modifier. When an interface is declared as public, it can be accessed by any class in any package. If no access modifier is used in the declaration, the interface is accessible only within the package in which it is declared.
  • interface interface_name: The keyword interface in the declaration tells the compiler that it is an interface declaration, and name of the interface is the one that follows the interface keyword. The name of an interface must be a valid identifier.
  • extends Super_interfaces: An interface can extend other interfaces in the same way as a class extends another class. The only difference between these two is that whereas a class is allowed to extend only one class, an interface can extend any number of interfaces. The extends keyword in an interface declaration tells the compiler that the interface extends the list of interfaces that follows the extends keyword.

The interface body

The interface body contains method declarations for all the methods included in the interface. A method declared within an interface does not have a body, i.e., the method declaration ends with a semicolon. This is because an interface does not provide implementations for the methods declared within it. Apart from method declarations, an interface contains constant declarations.

All the constants defined in an interface are by default static and final. Therefore, there is no need to explicitly use these keywords to declare variables inside the interface body. All methods and variables in an interface are implicitly public if the interface is declared as public. It is also not necessary to declare the methods as abstract as well. The following example demonstrates how to define an interface:

public interface MyInterface{
void method1();
int method2(int k);
int x = 1;
int y = 2;
}

Like this article? Share it with others
If you like this article, please leave a comment or subscribe this blog via RSS or via e-mail, Bookmark and share through your network. Click the AddThis button below. Thanks.
  • Share/Bookmark
Advertisement

Leave a Reply

uCertify.com | Our Company | Articles | Contact Us | News and Press Release | uCertify India | Entries (RSS)
MCSE: MCSA, MCTS, MCITP    JAVA Certification: SCJP, SCWCD    Cisco Certification: CCNA, CCENT    A+, Network+, Security+ Project+
Oracle Certification: OCP 11g, OCP 10g, OCA 11g, OCA 10g    CIW foundation    EC-212-32,    CISSP    Photoshop ACE CS4    Adobe Flash ACE, PMP, CAPM
© 2008 uCertify.com. All rights reserved. All trademarks are the property of their respective owners.