Re: Using Interfaces ?



TonyB wrote:
"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


Yes, you do seem to understand correctly. Indeed, it's very accurate to say you're declaring a "type" (although I wouldn't say "for a class's methods...see below.) Any class that implements an interface is said to have an "is a" relationship with that interface. Dog "is a" Animal, Cat "is a" Animal, etc. (where Animal is an interface that both Dog and Cat implement.)

One slight clarification. It's possible to have an empty interface, one without method signatures. So while it's usually the case that implementing an interface implies the existence of certain methods in the implementing class, it's not necessarily so. Such interfaces are often referred to as "tagging" interfaces; they simply "tag" a class as being a member of a group.

...Joe
.



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 ?
    ... TonyB wrote: ... "exampleinterface" declaring a list of methods. ... "example1class" which implements the interface methods 1 for 1. ...
    (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: [PATCH for review] ACPI: Create /sys/firmware/acpi/interrupts/ counters
    ... My expectation is that this is a shell interface for debugging, ... it is less convenient to cat the file and paste the results ... If sysfs can't handle it, ... Nope, no error checking, fun chance of blowing the memory buffer:( ...
    (Linux-Kernel)
  • [PATCH] Use proper seq_file api for /proc/scsi/scsi
    ... This patch impelements the proper seq_file interface which prints out ... cat /proc/scsi/scsi crashes with 'out of memory' ... static int scsi_add_single_device ...
    (Linux-Kernel)