Own implementation of an interface

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • gabriel

    Own implementation of an interface

    Hi

    Firstly i'd like to thank you for reading this and offer my appreciation
    for replies in advance.

    I've recently been writing a program which implements a user-defined API
    (Robocode to be exact). I, however, need to create my own implementation
    of an interface in order to perform some arbitary function.

    My question is, how would i go about creating my own instance of this
    interface so that the API utilises my methods instead of the blank,
    skeleton ones already provided?

    Is it simply Public interface myInterface extends APIInterfaceCLa ssNAMe?

    or is there some other method?

    As far as java goes i've always been a little hazey around the concept
    of interfaces, thus my confusion.
    thanks

    Gabriel
  • Luca Paganelli

    #2
    Re: Own implementation of an interface

    An interface is simply a definition of the way a class is expected to
    "act". It's a collection of definition of methods each of which must be
    implemented for a class to be said "implementi ng" that interface.

    To create an implementation of an interface either you can write it
    "implements " that interface and implement every single interface method,
    or you can write it extends another class that implements the interface
    and override those methods that must have custom implementation/behavior.

    Luca.

    gabriel ha scritto:
    Hi
    >
    Firstly i'd like to thank you for reading this and offer my appreciation
    for replies in advance.
    >
    I've recently been writing a program which implements a user-defined API
    (Robocode to be exact). I, however, need to create my own implementation
    of an interface in order to perform some arbitary function.
    >
    My question is, how would i go about creating my own instance of this
    interface so that the API utilises my methods instead of the blank,
    skeleton ones already provided?
    >
    Is it simply Public interface myInterface extends APIInterfaceCLa ssNAMe?
    >
    or is there some other method?
    >
    As far as java goes i've always been a little hazey around the concept
    of interfaces, thus my confusion.
    thanks
    >
    Gabriel

    Comment

    • Mark Rafn

      #3
      Re: Own implementation of an interface

      gabriel <petroleum@ethe r.netwrote:
      >My question is, how would i go about creating my own instance of this
      >interface so that the API utilises my methods instead of the blank,
      >skeleton ones already provided?
      >Is it simply Public interface myInterface extends APIInterfaceCLa ssNAMe?
      You probably want public myClass implements APIInterfaceNam e. And provide
      actual implementations of all the methods. If you write an interface, you can
      declare methods but not actually implement any of them.
      >or is there some other method?
      >As far as java goes i've always been a little hazey around the concept
      >of interfaces, thus my confusion.
      An interface is just a pure abstract class, with a slightly different syntax
      for creating the subclass - you implement it rather than extending it.

      Many frameworks provide both an interface and a convenience base class that
      partially implements it and lets you override just the part you want to change
      (like Swing's Action interface and AbstractAction class - you can implement
      Action or extend AbstractAction, depending on your needs).

      I haven't played with Robocode much, but I thought it did this as well - you
      extend Robot (or AdvancedRobot) and override the methods that get called at
      various events.
      --
      Mark Rafn dagon@dagon.net <http://www.dagon.net/>

      Comment

      • Lew

        #4
        Re: Own implementation of an interface

        gabriel <petroleum@ethe r.netwrote:
        >My question is, how would i go about creating my own instance of this
        >interface so that the API utilises my methods instead of the blank,
        >skeleton ones already provided?
        Mark Rafn wrote:
        You probably want public myClass implements APIInterfaceNam e.
        "public MyClass ..."

        Class names should start with an upper-case letter.

        - Lew

        Comment

        • Prasad

          #5
          Re: Own implementation of an interface

          Lew wrote:
          >gabriel <petroleum@ethe r.netwrote:
          >>My question is, how would i go about creating my own instance of this
          >>interface so that the API utilises my methods instead of the blank,
          >>skeleton ones already provided?
          >
          Mark Rafn wrote:
          >You probably want public myClass implements APIInterfaceNam e.
          >
          "public MyClass ..."
          >
          Class names should start with an upper-case letter.
          >
          - Lew
          Starting class names with an upper-case letter, is a widely used (good)
          convention. But I don't think it is required by the language.

          The following code compiles and executes just fine.

          public class sample
          {
          public static void main(String[] args)
          {
          System.out.prin tln("Hello World!");
          }
          }


          --
          Prasad

          Comment

          Working...