Re: Using Interfaces ?



TonyB wrote:
I've started java recently and I'm having some problems using interfaces in real world cases.
Say I have an interface, "exampleinterface" declaring a list of methods. I write a class, "example1class" which implements the interface methods 1 for 1. I can compile the class ok since all methods required by the interface are implemented in the class. I could write another class which also implements interface "exampleinterface" called "example2class".

Now if I want to use an object of type "example1class" in my code, I create it using "exampleobject = new example1class" where exampleobject is declared as type "example1class". Similarly I could use "example2class" also.

But I'm puzzled as to how to create an object that uses the interface "interfaceexample", and how to specify which of the two possible classes that implement the interface are actually used in my code ?

That's the whole point - when your code deals with an interface it does not care which actual implementation is used.
If you really want to know (say, for debugging), try
exampleobject.getClass().getName()
.



Relevant Pages

  • Re: Using Interfaces ?
    ... Say I have an interface, "exampleinterface" declaring a list of methods. ... Now if I want to use an object of type "example1class" in my code, ... public class AutomaticCar implements Car { ... public void drive() { ...
    (comp.lang.java.help)
  • Re: Using Interfaces ?
    ... "example1class" which implements the interface methods 1 for 1. ... An interface is really declaring a type for a class's methods, so that when I use a class based on that interface, it is type checked that it meets the requirements defined in the interface? ... Dog "is a" Animal, Cat "is a" Animal, etc. ...
    (comp.lang.java.help)
  • Using Interfaces ?
    ... I've started java recently and I'm having some problems using interfaces in ... Say I have an interface, "exampleinterface" declaring a list of methods. ... Now if I want to use an object of type "example1class" in my code, ...
    (comp.lang.java.help)
  • Re: intel compiler and [REFERENCE] keyword?
    ... would not change by declaring the argument to be a character array as ... of the interface, ... compile, but have a trailing underscore on the exported function. ... the Fortran 2003 features of C interoperability or intrinsic modules. ...
    (comp.lang.fortran)
  • Re: Error signature when calling GetScriptCount of IWMHeaderInfo3
    ... public static extern uint WMCreateEditor( ... public interface IWMMetadataEditor ... public interface IWMHeaderInfo3 ... This is definitely your problem--when declaring COM interfaces ...
    (microsoft.public.windowsmedia.sdk)