hey! i m reading java. and at one place i have read Audio interface in which play(), loop() functions are defined,but interface is something in which there can't be any method defined. so this must be abstract not interface.so please tell this problem, y we are calling this as interface. AudioClip interface, i am talking about!! i m have read it's API also, but here also it is defined as interface but at there only, play() like methods are defined in it. how is it possible?

Edited 4 Years Ago by gourav1: n/a

An interface is an agreement that a class with conform to certain things -- like having certain methods.

That way, the calling mechanism can be guaranteed it can call the play() method on your class if it is from the AudioClip interface.

http://en.wikipedia.org/wiki/Interface_(Java)

Edited 4 Years Ago by thines01: Added Link

An interface is an agreement that a class with conform to certain things -- like having certain methods.

That way, the calling mechanism can be guaranteed it can call the play() method on your class if it is from the AudioClip interface.

do u think so ? but the interface is a class in which we can only declare methods but we can;t define it. make it more clear what you said.
the link u have given is also favouring me. it is saying that it can't be initiatiated and can't have method definations

Edited 4 Years Ago by gourav1: n/a

take a look at this

in an interface, you can declare methods, you just don't give them a 'body'.

which means:

public void play();

is allowed

public void play(){
}

is not.

the implementation of these methods is done in each (non-abstract) class that implements the interface (or before, and gotten through inheritance)

There are many formats for sounds to play, and many possible sources to play them from. There are classes to play various formats from various sources, all different classes. But they all implement the same interface, so they all support the same calls to their play methods.
This means that you can get the right player class at the beginning of your code, but then just use the calls defined in the interface to play/pause/rewind etc without worrying about what kind of sound format or source it was.

also, you're not necessarily 'calling' the interface.
for instance:

public interface IntA{
  public void run();
}

public class ClaB{
  public ClaB(){

  }

  public void run(){

  }
}

if you have something like this, you can, for example, code:

public static void main(String[] args){
 IntA intName = new ClaB();
 // because each ClaB ís an intA
 intName.run();

}

also, you're not necessarily 'calling' the interface.
for instance:

public interface IntA{
  public void run();
}

public class ClaB{
  public ClaB(){

  }

  public void run(){

  }
}

if you have something like this, you can, for example, code:

public static void main(String[] args){
 IntA intName = new ClaB();
 // because each ClaB ís an intA
 intName.run();

}

intName.run(); i think this statement is valid only when some class implements interface. but in your code it is not implementing it. is it still correct ? secondly, i am ok with what you are saying but i m directly using play method without using object of any class which implements AudipClip interface. i have dirctly made an object of interface and call play from that object. it must first point to some object of any clss which implement interface. but it is nothing like that here.

Edited 4 Years Ago by gourav1: n/a

you don't 'inherit' an interface, you implement it.
inheriting means you don't have to write the code, there's a complete functional method, including body and logic in the class you're inheriting. you only need to write the method yourself if you would like to adjust the logic therein.
implementing an interface basicly means the interface decides on a number of methods you are obliged to implement and write logic for (or, at least an empty stub method)

you don't 'inherit' an interface, you implement it.
inheriting means you don't have to write the code, there's a complete functional method, including body and logic in the class you're inheriting. you only need to write the method yourself if you would like to adjust the logic therein.
implementing an interface basicly means the interface decides on a number of methods you are obliged to implement and write logic for (or, at least an empty stub method)

ya ya ! i have mistakely written that. i have corrected before your comment. please tell my answer please! i have posted in my last comment.

ah, yes off course, that was a mistake by my part :D
indeed, your ClaB needs to pass a Is-A test for IntA, so it needs to either inherit, or in this case, since it's an interface, implement it :)

so, indeed, it should be:

public class ClaB implements IntA{

public ClaB(){

}

public void run(){

}
}

the line intName.run(); would not give any problems, but the compiler would refuse the
IntA intName = new ClaB();
instantiation. totally right on that :)

ah, yes off course, that was a mistake by my part :D
indeed, your ClaB needs to pass a Is-A test for IntA, so it needs to either inherit, or in this case, since it's an interface, implement it :)

so, indeed, it should be:

public class ClaB implements IntA{

public ClaB(){

}

public void run(){

}
}

the line intName.run(); would not give any problems, but the compiler would refuse the
IntA intName = new ClaB();
instantiation. totally right on that :)

secondly, i am ok with what you are saying but i m directly using play method without using object of any class which implements AudipClip interface. i have dirctly made an object of interface and call play from that object. it must first point to some object of any clss which implement interface. but it is nothing like that here.PLEASE TELL ABOUT THIS SIR!! PLEASE!!

AFAIK that shouldn't even compile.
you can't instantiate the interface itself, unless you assign it to be one of the classes which implement it.
could you paste your code here, so we can have a look at it?

what's the exact code you used to do that?

try{
					AudioClip A = getAudioClip(new URL("C:\'Users\'gourav\'java_pr"));
 
		}catch(Exception e)
				{
 
	}
		A.play();
	}

also, in this tell me that did i have use getAudioClip function properly? is it correct ? does escape characters are neccessary ? and should i need to include file name in this path also?

Edited 4 Years Ago by gourav1: n/a

also, you may want to look a bit closer into escape characters in java. I think you're not getting the result you think you are.

Yes.
You should NEVER do this in a new program:

}catch(Exception e)
{
 
}

As stultuske has noticed, your getAudioClip is unlikely to work, and will throw an Exception. But you have told Java to tell you nothing about that, so you will never know what happened.
Here's what you should do:

}catch(Exception e)
{
    e.printStackTrace();
}

Here is an example I cooked that shows a technique I use for loading data from disparate systems or disparate repository types (file, web, database, etc.).

It makes the data load look and operate the same (on the outside) regardless of the repository type.

//IMaster.java
public interface IMaster
{
   boolean Load(Object strError);
}

...used by:

//LoadMasterList.java
import java.util.Vector;

public class LoadMasterList
{
   /////////////////////////////////////////////////////////////////////////
   // Loads classes with the IMaster interface from a Vector
   public static boolean Load(Vector<IMaster> lstMasters, Object strError)
   {
      // The original Load (from another language) has a callback for displaying the
      // object type being loaded.
      boolean blnRetVal=true;
      for(IMaster master : lstMasters)
      {
         if(!master.Load(strError))
         {
            // my original load (from another language) attaches the object type to
            // the error so the individual fail-er is known.
            blnRetVal = false;
            break;
         }
      }
      return blnRetVal;
   }
}

Dictates that all items loaded into the target vector will implement Interface IMaster and have method called Load.

This is designed to quit on the first non-loaded repository and return the text from an exception.

...and can now be used like this:

import java.util.*;

public class TestInterfaces
{
   private static NeatFileMaster masterNF = null;
   private static UsStateMaster masterUSA = null;
   private static UserMaster masterUser = null;

   public static void main(String[] args)
   {
      String strError = "";

      //////////////////////////////////////////////////////////////////////////
      // Collect all classes to be loaded
      Vector<IMaster> lstMasters = new Vector<IMaster>();
      // From file...
      lstMasters.add(masterNF = new NeatFileMaster("c:/science/java/Text.txt"));
      // From web...
      lstMasters.add(masterUSA = new UsStateMaster());
      // From database...
      lstMasters.add(masterUsers = new UserMaster());

      if(!LoadMasterList.Load(lstMasters, strError))
      {
         System.out.println("Could not load: " + strError);
         return;
      }

      System.out.println("NF Loaded " + masterNF.size() + " records");
      System.out.println("USA Loaded " + masterUSA.size() + " records");
      System.out.println("User Loaded " + masterUser.size() + " records");

   }
}

It keeps the use neat, clean is is intuitive (in my opinion).
This example shows three data repositories -- a file, a web page and a database --
all of which are loaded with the same mechanism.

Of course, the master is governed by the interface
//public class UsStateMaster extends Vector<UsState> implements IMaster

and the Loader is governed by another interface:

public interface IMasterLoader<T>
{
   boolean Load(T master, Object strError);
}

//public class UsStateLoader implements IMasterLoader<UsStateMaster>

There is also an underlying question of how and why would you use an interface.
Yes, it's overkill, but will eventually drive home the point.

the underlying question imho was why he was calling that specific interface directly (which actually, he wasn't).
not to say your post was bad or didn't contain useful information, I'm just not sure if the OP is at such level he will understand it at this point :)

This article has been dead for over six months. Start a new discussion instead.