Java Interface Tutorial

Home arrow Java Tutorials arrow Java Essentials arrow Java Interface Tutorial
Java Interface Tutorial Print E-mail
Contributed by Teddy   
Thursday, 28 September 2006

What Is a Java Interface?

Class is the fundamental unit of programming in the Java programming ,  type is the fundamental unit of object-oriented design.While classes define types, it is very useful and powerful to be able to define a type without defining a class. Interfaces define types in an abstract form as a group of related methods with empty bodies

 

A Simple Interface Example

An interface declaration is similar to a class declaration, except that the keyword interface is used instead of class. See following example:

public interface Playable
{
void play();
}

Implementing an interface

To implement an interface, a class must do two things:
  • It must specify an implements clause on its class declaration.
  • It must provide an implementation for every method declared by the interface.

For example, here’s a class that implements the Playable interface:


public class Basketball implements Playable
{
public void play()
{
}
}

Differences between Interfaces and inheritance

The interface itself doesn’t provide code that implements any of its methods. An interface is just a set of method and field signatures. In contrast,a base class can provide the implementation for some or all of its methods.
A class can have only one base class. However, a class can implement as many interfaces as necessary.

Last Updated ( Thursday, 28 September 2006 )

  home              contact us

 

©2006-2008 DeveloperZone.biz   All rights reserved     powered by Mambo Designed by Siteground