Re: Using Interfaces ?



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

Yes.

One small note: there is a nearly universal convention in Java for laying out source code and for nomenclature.
<http://java.sun.com/docs/codeconv/html/CodeConvTOC.doc.html>

Class names should start with an upper-case letter, variable and method names with a lower-case letter. Each subsequent word part within a name also begins with an upper-case letter.

As a matter of engineering, it is bad practice to include things like "class" in a class name, "object" in an object name, "interface" in an interface name.

You might refer a class to an interface or vice versa, and to have to change the name is silly. All objects are objects, so saying "object" is unnecessary.


- Lew
.



Relevant Pages

  • Re: Own implementation of an interface
    ... interface so that the API utilises my methods instead of the blank, skeleton ones already provided? ... Class names should start with an upper-case letter. ...
    (comp.lang.java)
  • Re: interfaces
    ... An interface will allow you to publish a standard, um, interface, for ... interface can use the functionality. ... So if you are writing a class "smee" that you want to be able to sort, ... and implement all the required methods for those interfaces, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: ASP.NET Generic template
    ... As long as your base form implements the required methods, ... course this.mainObject is declared of type _BaseForm. ... Otherwise interface might help, but abstract class is still better. ...
    (microsoft.public.dotnet.languages.csharp)