Re: Using Interfaces ?



"TonyB" <tonyb@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:dFcEh.6455$ud2.5085@xxxxxxxxxxxxxxxxxxxxxxxxx
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 ?

Thanks
Tony

I think I've answered my own question. 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 ?
So I could say
exampleinterface object 1 ;
exampleinterface object 2 ;
....
object1 = new example1class();
object2 = new example2class();

If this compiles I know that both objects implement the required methods.
Do I understand this correctly ?
Tony


.



Relevant Pages

  • 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: creaping coupling......
    ... but it is a more strict contract than required. ... "I am capable of implementing things via interface XYZ i.e. ... consider specifically that we compile, deploy and then we delete a method to ... IWriteable pipe = new Pipe; ...
    (comp.object)
  • Re: ActiveX.exe question
    ... > the public interface still need registered in order to use them. ... Compile AXExe.exe against a Share on the TestPC ... Open a Standard-Exe-Project on your DevelopPC and name it AXTest ... Compile AXTest.exe against the Share on the TestPC ...
    (microsoft.public.vb.general.discussion)
  • Re: RAD vs. performance
    ... thus they fulfull the number interface and can be used interchangeably. ... I'm not about to ditch statically typed languages ... Because you need whole-program optimisation for a start. ... The benefits of static typing come at compile time. ...
    (comp.lang.misc)
  • Re: Is Direct3D a rip-off of OpenGL?
    ... People who think that a method change in a NEW INTERFACE in DirectX really ... Moving to use a NEW INTERFACE, ... > ago simply isn't going to compile with the latest SDK, ...
    (comp.graphics.api.opengl)