Game Programming and Development Tools

(Java) Interfaces and Abstract functions, what the heck!??! – Realm Master

Realm Master

Member

Posts: 1971
From: USA
Registered: 05-15-2005
I just don't get it.

I don't see WHY in normal programming (well, not normal, maybe like a video game) or HOW you would use an abstract functoin.

I know of interfaces, but I don't understand how they WORK.

Like for instance, (if i can recall the correct name) a function like mouseWheelRoll(MouseWheel e) (made up off the top of my head, but i know there's stuff like it) is an abstract function, so you define it within the curly braces. What I'm wondering is how do you get it so that mouseWheelRoll(MouseWheel e) only functions when the mouse wheel is moved or rolled or stuff.

I'm going to a 4th of july thing with my family, so I'll post more later.

I'm thinking only a very limited amount of people are going to know what im talking about (Namely, HanClinto and Luke)

-RM

------------------
yeah, im a little crazy

Blessed are those who suffer for doing what is right.
The kingdom of heaven belongs to them.-Matthew 5:10

PM ME YOUR DESCRIPTION OF ME! ILL PUT IT HERE!

Here's all the comments!

P.S. I HATE 640x480!!!!!!

ArchAngel

Member

Posts: 3450
From: SV, CA, USA
Registered: 01-29-2002
i might be totally off, but if I remember correctly, abstract methods and classes needs to be inherited and defined. they only serve as a superclass.

so you'd need to to define it yourself.
it's used to keep a congruity and to serve as a basis for a group of other classes.

interfaces is kind of a protocol that "says" your class has these certain features which can be accessed.
you say your class implements a certain interface, and then you're required to include certain methods.

so, if your class implements the Comparable interface, you're required to provide a couple methods, like compareTo() // i think, too lazy to check up on the api // that allow it to be compared. so anybody can look and see your class is Comparable and will know they can use the compareTo() method.

you'll have to give the name of the class where your method is from.
I don't know too much of the mouse (besides the MouseAdaptor class) to give you the function.
MouseMotionAdaptor might be the class your looking for. look it up on the API and play around with it.

------------------
"Patience, my good citizen, patience. It's bad enough to rob a man of his dream"
-Sydney Carton, Tale of Two Cities
Soterion Studios

luke

Member

Posts: 311
From: I use your computer as my second Linux box
Registered: 10-30-2005
Alrighty, I'd just LOVE to give you some pointers here lol

#1 why have abstract methods/interfaces
A: An interface allows otherwise unrelated object to deal with an 'event' (actually, that event is just the abstract function being called) in their own way. Eg, your button class implements ActionPerformed(ActionEven e) so that when this function recieves an ActionEvent for being clicked it does what the button needs to do, but your text box also implements this same ActionPerformed(ActionEvent e) so that it only changes keyboard focus to itself in 'reaction' to the same 'event'. Conclusion: Abstract methods/interfaces allow different objects to 'react' in a way suited to them.

#2 How do said abstract methods get called?
A: Normally the abstract method gets called by code outside of your own (assuming that you use external ab methods/interfaces) like in swing, Swing automatically calls your button's or text box's ActionPerformed(ActionEvent e) and passes a filled out ActionEvent for your object to 'react' too.
If you define an interface/ab method for yourself then it would be like this:

 
//Killer game App
public interface GameObject /*Allow user to inherit what they want w/ minimal interference */
{
void update();
void draw();
bool isAlive();
int GUID();
...
}

//later on:
...
for(int i = 0; i < MaxGameObjects; i++)
{
if(GameObjects[i].isAlive())
{
GameObjects[i].draw();
GameObjects[i].update();
}
System.out.println (GameObjects[i].GUID());
}


Abstraction allows objects to 'react' in different ways that suit their needs/function.

------------------
I reject your reality and substitute my own!

[This message has been edited by luke (edited July 04, 2006).]

[This message has been edited by luke (edited July 04, 2006).]

[This message has been edited by luke (edited July 04, 2006).]

Realm Master

Member

Posts: 1971
From: USA
Registered: 05-15-2005
O.O

wow. Way more than i needed, and probalby way more than i understand, but thanks! I didn't know you knew java arch!

Give me a few days, and i'll figure it out!

thanks for the help!

------------------
yeah, im a little crazy

Blessed are those who suffer for doing what is right.
The kingdom of heaven belongs to them.-Matthew 5:10

PM ME YOUR DESCRIPTION OF ME! ILL PUT IT HERE!

Here's all the comments!

P.S. I HATE 640x480!!!!!!

ArchAngel

Member

Posts: 3450
From: SV, CA, USA
Registered: 01-29-2002
yeah, lotta Comp Sci classes are in java, so I took around 4 of them.

Luke and Han are better, but if you need help, just ask.

------------------
"Patience, my good citizen, patience. It's bad enough to rob a man of his dream"
-Sydney Carton, Tale of Two Cities
Soterion Studios

jestermax

Member

Posts: 1064
From: Ontario, Canada
Registered: 06-21-2006
Its not just java arch; its object oriented design, java just happens to be pure object oriented. I can help if you want, i'm almost done my computer science program and i've tutored programming before (not boasting here, just letting you know).

Abstract functions are a function with a name but no code. Which means if you're going to instantiate a class with one then you need to override that function.

Interfaces allow you to use a design pattern called 'polymorphism'. This means that different objects act in a similar fashion. Example:
A Circle is a Shape, A Square is a Shape, therefore, Shape is an interface.

It's a little difficult to grasp sometimes at first but the more you think about, the more you'll understand it.
Theres my 3 cents (you guys get an extra cent )

SSquared

Member

Posts: 654
From: Pacific Northwest
Registered: 03-22-2005
It just so happens Java implemented their events/actions as Interfaces, but an Interface does not really have anything to do with events. The event model described above is just one use of an Interface.

Interfaces and abstract classes are an OO concept and are not necessarily Java specific. For example, an abstract class in C++ is created by having at LEAST one method with no implementation (pure virtual), but contains some methods WITH implementation. A Java interface is simply a class where ALL methods have NO implementation. So, an interface is really a class containing no implementation.

An example of an abstract class may be an Animal class containing methods like:

setColor - sets the animal's color
setFeet - sets the animal's number of feet
setSound - sets the animal's sound,
setFood - set the animal's favorite food

You may look at setSound and setFood as being very specific to a given animal, so you may desire to have NO default implementation in the Animal base class. Your derived Zebra, Lion, and Seal will be REQUIRED to implement these methods. The other listed methods will have default implementation in the Animal class.

An interface defines a set of behaviors you may want in your classes. It is often used to define similar behavioral ideas between completely non-related classes, but is by no means a requirement. Let's say you are creating a game and have many different game objects (enemy, player, crate, laser shot). You may want to create an interface which defines methods like collision, movement, physics, etc. Each of these objects implements this class, requiring EACH one to create their own version of these methods for THEIR OWN needs.

Or perhaps you want to create a calculator and a color creator. You can create an interface with Add, Subtract, Divide, Multiply which take an integer value. The calculator will do its associated function and return the result value (3+4 returns 7, for example). The same is done for the color creator but the integer value may actually represent an RGB value.

So, an interface is simply a way to define behavior you desire and FORCES you to IMPLEMENT EACH method in the interface.

How do you access these? The same way you access any other method. If I have my calculator class which implemented Add, I will call calculator.Add(3, 4).

Another purpose for Interfaces is to allow for Remoting. This, to me, is one of the coolest things. Remoting allows you to talk to a class running in a different process, or on a completely different machine. For remoting, the jave Interface defines the public access to the remote object. So, I can be running an application on my machine, and create a Calculator locally, but will actually access the calculator instance on steveth45's machine. The interface says, I can only talk to steveth45's calculator object using Add, Subtract, Multiply, and Divide. I do not have access to any other of Calculator's methods.

One reason Sun chose to implement Interfaces how they did is to remove potential problems with multiple inheritance (something C++ has). Notice how in Java you can only derive from ONE class. If you want more behavior, anything else must be an interface you implement. Since an Interface requires you to implement EACH item, you will often have several methods in your class which you may actually never use.

Sorry this is kind of long.

luke

Member

Posts: 311
From: I use your computer as my second Linux box
Registered: 10-30-2005
True an interface doesn't really have anything to do with events; I was only using it as an example.

SSquared is quite correct about the pure virtual function; in fact to make an interface following the characteristics of a Java interface, you could do this:


class myClassInterface
{
public:
virtual void somefunction() =0;
}

by just doing public pure virtual functins, you can make an interface(you can also have some consts in an interface too).


But to continue the 'event' (take note of '') analogy:
the 'event' is the calling of the abstract method on a class, the class then 'reacts' (processes thru the abstract method) to the event.

I only like the 'event' example because I just like event driven programming very much; interfaces allow so much more than just some simple 'event' driven programs.

For example, say you have a class that does graphics but you update it often and you want earlier versions to benefit/not break from the upgrades in Java you could just make many different interfaces and have one class implement them all like


public interface version1
{
void draw(Image image);
}
...
public interface version2
{
void collisiondetection(Object3d o3d1, Object3d o3d2);
}
...
public interface version3
{
int status();
}
...
...
//In the actual 'device' or whatever you just do this
public class GraphicsDevice implements version1, version2, version3
{
void draw()
{
...
}
void collisionDetection(Object3d o3d1, Object3d o3d2)
{
...
}
int status() {.... return int;}
}

now when using this little interface practice that I have just demonstrated, as long as you maintain the same prototypes, your code will be backwards compatible.
Microsoft has a standard called COM (Component Object Module or somehting like that I think) that is backwards compatible and works entirely through interfaces, and of which I know no more about. Fyi DirectX is COM formated.

Please note that instead of using an interface your could just skip that and use a class but im just trying to explain some uses of Interfaces lol.
In fact I would never do something like that ^^ mostly since It is only practical if you are making multiple devices or such... and even then i'd probably go with inheritance...


Oh and I also like the usefullness of remoting myself, and it is also Javas workaround for multi inheritance. Kudos to SSquared.
------------------
I reject your reality and substitute my own!

[This message has been edited by luke (edited July 05, 2006).]

[This message has been edited by luke (edited July 05, 2006).]

jestermax

Member

Posts: 1064
From: Ontario, Canada
Registered: 06-21-2006
inheritance is good if you're using it for the right reasons. theres a bazillion articles on the internet stating the reasons for that so i won't get into it now. Interfaces have a ton of nifty uses and are very good for keeping up good design. Yeah, you could easily get around without them but sometimes that can lead to poorly written code that noone can read.

Cheers