Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
    Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Requirement of Interface in Java

Posts: 4
  • Mark post as helpful
  • send pies
  • Report post to moderator
Interface can fully abstract a class interface from its implementation that�s using this we can specify what a class do but not how it does it. Interfaces are similar to classes but they don�t have instance variables and there methods are declared without a body. Once it is defined, any number of classes can implement interface and one class can implement any number of interfaces. Each class is free to determine the details of its own implementation.
For example:

Interface callback{
Void callback(int param)

Access is either public or not specified, if not specified then it is considered to be default and interface is available to other members of the package. Variables can be declared inside interface declaration. They are however implicitly final and static that is they cannot be changed by the implementing class. Variable and method are public if access declaration is public.

Implementing Interfaces

Class client implements callback{
Public void callback(int p){
System.out.println(�callback called with � +p);

If a class implement two interface that declare the same methode then the same method will be used by clients of either interface.
A class which implements interface can be define additional members of its own class.

Accessing Implementations through interface References

We can also declare variable as object references that use an interface. Any instance of a class that implement the declared interface can be referred to by such a variable.When we call a method through one of the references, the correct version will be called based on the actual instance of the interface being referred to. The methode to be executed is looked up dynamically at run time ,allowing classes to be created later than the code which calls method on them.

For Examle:
Class testIface
Public static void main(String args[]){
Callback c= new client();
Variable c is declared to be of the interface type callback, but it is assigned as instance of client. C can be used to access the callback() method ,it cannot access any other member of client class .An interface variable only has knowledge of the methods declared by its interface declaration, this c could not be used to access non interface method since it is defined by client but not callback.
If a class includes an interface but doesn�t fully implement the methods defined by that interface, then that class must be declared as abstract.

Abstract class Incomplete implements callback{
Int a,b;
Void show(){
System.out.println(a+ � �+b);

Interface tool is an important part of java program environment. In C++ we have multiple inheritance so a class can inherit from many classes but since it causes a lot of ambiguity this procedure is avoided in java and instead we use interfaces which once declared can be called in any number of classes any number of times.
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Report post to moderator
If this is a question, I've missed it...
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Report post to moderator
see this post
Talk sense to a fool and he calls you foolish. -Euripides A foolish tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic