Results 1 to 4 of 4

Thread: Interfaces in Java

  1. #1
    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.

  2. #2
    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 !
          */

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    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);
    }

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    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");
        }
    }

Similar Threads

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

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,903,443.69884 seconds with 16 queries