- Java Programming Basics
- Java Tutorial
- Java Overview
- Java Environment Setup
- Java Program Structure
- Java Basic Syntax
- Java First Program
- Java Constants
- Java Separators
- Java Keywords
- Java Data Types
- Java Data Types
- Java Integers
- Java Floating Point
- Java Characters
- Java Booleans
- Java Numbers
- Java Programming Variables
- Java Variables
- Java Variable Types
- Java Variable Scope
- Java Type Conversion
- Java Type Casting
- Java Auto Type Promotion
- Java Type Promotion Rules
- Java Programming Arrays
- Java Arrays
- Java One Dimensional Array
- Java Multidimensional Array
- Java Programming Operators
- Java Operators
- Java Arithmetic Operators
- Java Increment Decrement
- Java Bitwise Operators
- Java Left Shift
- Java Right Shift
- Java Relational Operators
- Java Boolean Logical Operators
- Java Ternary(?) Operator
- Java Operator Precedence
- Java Control Statements
- Java Decision Making
- Java if if-else if-else-if
- Java switch Statement
- Java Loops
- Java while Loop
- Java do-while Loop
- Java for Loop
- Java for-each Loop
- Java Nested Loops
- Java break Statement
- Java continue Statement
- Java Class Object Method
- Java Classes and Objects
- Java Class
- Java Object
- Java new Operator
- Java Methods
- Java Constructors
- Java this Keyword
- Java Stack
- Java Overloading Recursion
- Java Method Overloading
- Java Constructor Overloading
- Java Object as Parameter
- Java Call by Value Reference
- Java Returning Objects
- Java Recursion
- Java Modifier Types
- Java Encapsulate Poly String
- Java Encapsulation
- Java Polymorphism
- Java Nested Inner Class
- Java Strings
- Java Command Line Arguments
- Java Variable Length Arguments
- Java Inheritance Abstraction
- Java Inheritance
- Java super Superclass
- Java Multilevel Hierarchy
- Java Method Overriding
- Java Abstraction
- Java Packages Interfaces
- Java Packages
- Java Access Protection
- Java Import Statement
- Java Interfaces
- Java Programming Exceptions
- Java Exception Handling
- Java try catch
- Java throw throws
- Java finally Block
- Java Built In Exceptions
- Java Exception Subclasses
- Java Chained Exceptions
- Java Multithreading
- Java Multithreading
- Java Thread Model
- Java Main Thread
- Java Create Thread
- Java Thread Priorities
- Java Synchronization
- Java Inter Thread Communication
- Java Suspend Resume Stop Thread
- Java Get Thread State
- Java Enum Autobox Annotation
- Java Enumerations
- Java Type Wrappers
- Java Autoboxing
- Java Annotation
- Java Marker Annotations
- Java Single Member Annotation
- Java Built In Annotations
- Java Type Annotations
- Java Repeating Annotations
- Java Data File Handling
- Java Files I/O
- Java Streams
- Java Read Console Input
- Java Write Console Output
- Java PrintWriter Class
- Java Read Write Files
- Java Automatically Close File
- Java Programming Advance
- Java Date and Time
- Java Regular Expressions
- Java Collections Framework
- Java Generics
- Java Data Structures
- Java Network Programming
- Java Serialization
- Java Send Email
- Java Applet Basics
- Java Documentation
- Java Programming Examples
- Java Programming Examples
Java Interface
Using the keyword interface, you can totally abstract a class interface from its implementation i.e., using interface, you can specify what a class must do, but now how it does it.
Interfaces are syntactically similar to classes, but they lack instance variables, and, as a general rule, their methods are declared without any body. In practice, this means that you can define interfaces that do not make assumptions about how they are implemented. Once it is defined, any number of classes can implement an interface. Likewise, one class can implement any number of interfaces.
How to implement interface ?
To implement an interface, a class must provide complete set of methods required by the interface. However, each class is free to specify the details of its own implementation. By providing the interface keyword, Java allows you to fully utilize the "one interface, multiple methods" aspect of polymorphism.
Why interfaces are designed ?
Interfaces are designed to support the dynamic method resolution at run time. Normally in order for a method to be called from one class
to another, both the classes need to be present at compile time so the Java compiler can check to ensure that the method signatures are
compatible. And this requirement by itself makes for a static and non-extensible classing environment. Necessarily in a system like this,
functionality gets pushed up higher and higher in class hierarchy so that the mechanisms will be available to more and more subclass. Interfaces
are designed to avoid this problem. They disconnect the definition of a method or set of methods from the inherit hierarchy. Since interfaces
are in a different hierarchy from classes, it is possible for the classes
which are unrelated in terms of the class hierarchy to implement the same interface. This is where the actual power of interfaces is realized.
Define an Interface
An interface is defined much like a class. Following is the simplified general form of an interface :
access interface name { return-type method-name1(parameter-list); return-type method-name2(parameter-list); type final-varname1 = value; type final-varname2 = value; //... return-type method-nameN(parameter-list); type final-varnameN = value; }
When no access modifier is included, default access results, and the interface is only available to other members of the package in which interface is declared.
When interface is declared as public, the interface can be used by any other code. In this case, the interface must be only public interface declared in the file, and the file must have the same name as interface. name is the name of the interface, and can be any valid identifier. Notice that the methods which are declared have no bodies. Then they must end with a semicolon after the parameter list. They are, essentially, abstract methods. Each class that includes such an interface must implement all of the methods.
As the general form shows, variables can be declared within of interface declarations. They are implicitly final and static, meaning they cannot be changed by the implementing class. And they must also be initialized.
Interface Definition Example
Following is an example of an interface definition. It declares a simple interface having one method called trythis() that takes a single integer parameters.
interface Trythis { void trythis(int par); }
Implement Interfaces
When an interface has been defined, one or more classes can implement that interface.
To implement an interface, simply include the implements clause in class definition, and then create the methods needed by the interface.
Below is the general form of a class that includes the implements clause :
class classname[extends superclass] [implements interface[,interface...]] { // body of the class }
If a class implements more than one interface, the interfaces are separated by a comma. If a class implements two interfaces that declare the same method, then the same method will be used by the clients of either the interface.
Note - The methods that implement an interface must be declared as public.
Note - The type signature of the implementing method must watch exactly the type signature specified in the interface definition.
Java Interface Example
Following is a small example class that implements the Trythis interface as shown earlier :
class Customer implements Trythis { /* implements Trythis's interface */ public void trythis(int par) { System.out.println("trythis called with " + par); } }
Notice here that the trythis() method is declared using the public access modifier.
It is permissible and common for the classes that implement interfaces to define the additional members of their own. For instance, the following version of the Customer implements trythis() and adds the nonIfaceMeth() method:
class Customer implements Trythis { /* implement Trythis's interface */ public void trythis(int par) { System.out.println("trythis called with " + par); } void nonIfaceMeth() { System.out.println("Classes that implement interfaces may also define other members, too."); } }
« Previous Tutorial Next Tutorial »