Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2006

Tip: Looking for answers? Try searching our database.

Own implementation of an interface

Thread view: 
gabriel - 27 Oct 2006 22:57 GMT
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 APIInterfaceCLassNAMe?

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 - 28 Oct 2006 00:25 GMT
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 "implementing" 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
>
[quoted text clipped - 18 lines]
>
> Gabriel
Mark Rafn - 29 Oct 2006 04:02 GMT
>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 APIInterfaceCLassNAMe?

You probably want public myClass implements APIInterfaceName.  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/>
Lew - 30 Oct 2006 01:06 GMT
>> 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?

> You probably want public myClass implements APIInterfaceName.  

"public MyClass ..."

Class names should start with an upper-case letter.

- Lew
Prasad - 01 Nov 2006 11:59 GMT
>>> 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,
[quoted text clipped - 7 lines]
>
> - 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.println("Hello World!");
     }
}

Signature

Prasad



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.