Valhalla Legends Forums Archive | Java Programming | Class Interface

AuthorMessageTime
ObsidianWolf
What is a Class Interface? And can anyone give me an example of how to use one? Uber Chobo on this one.
January 30, 2004, 1:28 AM
Tuberload
A class interface is a abstract class without an implementation, or a way of knowing what methods/variables you can/have to use.

An example would be with a battle.net bot hierarchy. You have your good and your bad bots, but both inherit some characteristics that make them a bot. So both types of bot inherit the need to connect, logon, send/receive data, display data, yada, yada, from the generic bot object.
[code]
Bot - Interface/Abstract/Class
/ \
Good Bot Bad Bot - Bot Subclass's
[/code]

A code example might look like the following:
[code]
public interface Bot
{
public boolean connect (byte[] host, int port);
public boolean logon (byte[] username, byte[] password);
public void send (byte[] data);
public byte[] recv();
}

public class BadBot implements Bot
{
// All of the methods in the bot interface must be implemented
}

public class GoodBot implements Bot
{
// The same thing for him...
}
[/code]

Now whether you program a bad bot, or a good bot you have to implement these basic functions to be a bot. This example would not be of much use in a real world application. To be really useful you have to combine it with polymorphism. Basically if a class implements an interface, any other class that wants to use it will be guaranteed access to the methods declared in the interface. This can be used to create plug-ability, or "future proof software" that allows new objects to be inserted into the program, without modifying existing code.

Edit: Minor code modification.

January 30, 2004, 3:14 AM
iago
Think of the interface comparable. If any class implements comparable, it's guarenteed to have a compareTo() function, where it will be compared to another object of the same type. By implementing it, everybody knows your class can be compared to itself, which is great for many, many different classes.
January 30, 2004, 3:44 AM
Null
Nice example Turbo , lots of people can relate to that one , GJ
January 30, 2004, 7:29 AM
Tuberload
[quote author=NuLL link=board=34;threadid=4995;start=0#msg41803 date=1075447743]
Nice example Turbo , lots of people can relate to that one , GJ
[/quote]

Turboload...hmm...kind of has a ring to it. ;D
January 30, 2004, 8:26 AM
Kp
[quote author=iago link=board=34;threadid=4995;start=0#msg41793 date=1075434243]
Think of the interface comparable. If any class implements comparable, it's guarenteed to have a compareTo() function, where it will be compared to another object of the same type. By implementing it, everybody knows your class can be compared to itself, which is great for many, many different classes.[/quote]

It's worth noting that there's no particular rule which says your object's compareTo method can't support comparing it to types of objects other than itself, although you'd have to know something about the type to which it is being compared for the result to be meaningful.

As an example, if I wrote my own class to wrap an int, I might want to have my compareTo method detect the case that someone tried to compare my class to a builtin Integer class. I could use the Integer class's interface to get at its value, so that I can return the result of comparing my contained integer with the int contained in the Integer class. Thus, a meaningful result can be obtained by using my compareTo with my object and an Integer object.
January 30, 2004, 9:45 PM
ObsidianWolf
thanks
January 30, 2004, 10:07 PM

Search