• Welcome to Valhalla Legends Archive.
 

Class Interface

Started by ObsidianWolf, January 29, 2004, 07:28 PM

Previous topic - Next topic

ObsidianWolf

What is a Class Interface? And can anyone give me an example of how to use one?  Uber Chobo on this one.

Tuberload

#1
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.

         Bot           - Interface/Abstract/Class
       /     \
Good Bot      Bad Bot  -  Bot Subclass's


A code example might look like the following:

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...
}


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.

Quote"Pray not for lighter burdens, but for stronger backs." -- Teddy Roosevelt
"Your forefathers have given you freedom, so good luck, see you around, hope you make it" -- Unknown

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.
This'll make an interesting test for broken AV:
QuoteX5O!P%@AP[4\PZX54(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*


effect

Nice example Turbo , lots of people can relate to that one , GJ
Quote from: Mangix on March 22, 2005, 03:03 AM
i am an expert Stealthbot VBScript. Recognize Bitch.

Tuberload

Quote from: NuLL on January 30, 2004, 01:29 AM
Nice example Turbo , lots of people can relate to that one , GJ

Turboload...hmm...kind of has a ring to it. ;D
Quote"Pray not for lighter burdens, but for stronger backs." -- Teddy Roosevelt
"Your forefathers have given you freedom, so good luck, see you around, hope you make it" -- Unknown

Kp

Quote from: iago on January 29, 2004, 09:44 PM
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.

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.
[19:20:23] (BotNet) <[vL]Kp> Any idiot can make a bot with CSB, and many do!

ObsidianWolf