Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 07-11-2009
Member
 
Join Date: Nov 2009
Posts: 446
Interfaces in Java

Hi
I am new to java and I want to clear the interface concept more clearly. Java is a great programming language. I like to code java programs. So, can any one help me in explaining interfaces in java, some interface examples will do. If you post some simple examples I can get it working by my side. hoping for a reply.
Reply With Quote
  #2  
Old 07-11-2009
Member
 
Join Date: Apr 2008
Posts: 1,948
Re: Interfaces in Java

Interface is a group of related methods with empty bodies. Check this simple example you will be more clear with it. Every thing is explained in the code.
Code:
/*
       Java Interface example.
       This Java Interface example describes how interface is defined and
       being used in Java language.
       Syntax of defining java interface is,
       <modifier> interface <interface-name>{
         //members and methods()
       } 
      */
      //declare an interface
      interface IntExample{
      /*
        Syntax to declare method in java interface is,
        <modifier> <return-type> methodName(<optional-parameters>);
        IMPORTANT : Methods declared in the interface are implicitly public and abstract.
        */
      public void sayHello();
      }
      }
      /*
      Classes are extended while interfaces are implemented.
      To implement an interface use implements keyword.
      IMPORTANT : A class can extend only one other class, while it
      can implement n number of interfaces.
      */
      public class JavaInterfaceExample implements IntExample{
      /*
        We have to define the method declared in implemented interface,
        or else we have to declare the implementing class as abstract class.
        */
      public void sayHello(){
      System.out.println("Hello Visitor !");
      }
      public static void main(String args[]){
      //create object of the class
      JavaInterfaceExample javaInterfaceExample = new JavaInterfaceExample();
      //invoke sayHello(), declared in IntExample interface.
      javaInterfaceExample.sayHello();
      }
      }
      /*
      OUTPUT of the above given Java Interface example would be :
      Hello Visitor !
      */
Reply With Quote
  #3  
Old 07-11-2009
Member
 
Join Date: May 2008
Posts: 2,008
Re: Interfaces in Java

This is a simple example of Interface. The below example is like a syntax of interface, it explains how a basic interface looks like.
Code:
interface Bicycle {

       void changeCadence(int newValue);   

       void changeGear(int newValue);

       void speedUp(int increment);

       void applyBrakes(int decrement);
}
Reply With Quote
  #4  
Old 07-11-2009
Member
 
Join Date: Apr 2008
Posts: 2,001
Re: Interfaces in Java

In this class I implement Java shape interface in the circle class, up-casting for the object "circleshape". In Interfaces functions are public and abstract ,and fields are public and final. View the following example
Code:
public class Main {           
    public static void main(String[] args) {
                 shape circleshape=new circle();
             circleshape.Draw();
    }
}
interface shape
{
     public   String baseclass="shape";
     public void Draw();
}
class circle implements shape
{
    public void Draw() {
        System.out.println("Drawing Circle here");
    }
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Interfaces in Java"
Thread Thread Starter Forum Replies Last Post
What are an interfaces to CRM applications Deward Technology & Internet 5 14-02-2011 05:31 PM
Need the best audio interfaces Cajetan Hardware Peripherals 3 08-02-2011 06:02 AM
Professional Soundcards/Interfaces Ravana Windows Software 8 27-09-2010 11:58 PM
Problem with Inheritance and interfaces TechGate Software Development 5 27-02-2010 02:25 AM
Importance of c# Interfaces Linoo Software Development 4 08-02-2010 07:24 PM


All times are GMT +5.5. The time now is 04:17 PM.