Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm just now moving from a beginner to intermediate level android programmer in the java language. i can successfully write a game framework of classes that work together to accomplish a task beyond basic things, like hello world. but i'm having issues with some pretty basic OOP concepts; When should i derive from an abstract class? When is it more efficient to use an Interface instead of simply sub classing a parent?

Basically, between extends, implements, and the abstract keywords, which keywords should be used instead of the others? i'm not looking for a basic definition, as i know them. i need to no when and why i should apply them to my code? what advantages does one have over the other? which is best for game development?


Ok specifically what i'm talking about is that i have a game framework that looks like this:

Graphics interface

class screen implements graphics

Throughput interface

class keyreader implements throughput

GameThread extends thread

abstract character

class player extends character

class npc extends character

... are these good ways to write a game framework or should i stick to one type of OOP inheritance?

share|improve this question

closed as off topic by Tim Holt, Patrick Hughes, Tetrad Apr 6 '12 at 19:27

Questions on Game Development Stack Exchange are expected to relate to game development within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

1  
they all should be used, because they all have different uses. –  DampeS8N Apr 6 '12 at 18:04
2  
The basic definition kind of defines when you use them. They are all tools used in specific situations and the situation dictates which one you use. So asking for a guide on how to chose between them demonstrates you are not aware of the "basic definition". Anyways, since this question can be interpreted as "how do I do OOP", this question can not be adequately answered without wrinting a book on the topic and many people have done so already –  ClassicThunder Apr 6 '12 at 18:11
    
Pretty off topic for this list. Belongs in a programming stack, not game dev as it looks like the only real game question is, "Which is best for game development?" –  Tim Holt Apr 6 '12 at 18:33
    
well I've been using all three in different situations, but i'm wondering if my code would be more efficient in terms game programming to stick to one –  kdavis8 Apr 6 '12 at 21:15

1 Answer 1

up vote 4 down vote accepted

I'll try to approach this from a simplified and informal point of view. You should still turn to the bibliography and take some time to study object oriented concepts correctly. These are also only the most common uses, although there's other situations for them.

Abstract Classes

Should be used when it doesn't make sense for an instance of the base class to be created. Just take a look at the common begginer's example that presents a shape hierarchy:

enter image description here

You can find a way to describe any of the subclasses in very concrete terms (e.g. an ellipse has a center and two radius, a polygon is a collection of vertices, the others are specializations of the previous cases). On the other hand, a shape is an abstract concept in the sense that it can refer to any of the other cases, but you can't find a concrete definition for it. In sum, ask yourself the question:

Is there enough information to create a meaningful instance of this class?

And if the answer is no, make it abstract.

Interfaces

Should be used when you have a set of operations that you would like to see supported by several classes, but you want those classes to still be able to inherit (i.e. extend) from other classes. Classes in java can't inherit from multiple classes, but they can implement multiple interfaces.

So let's say you had this interface and these two classes (sorry if the syntax is wrong, I'm a bit rusty on Java):

interface ITalk { void Talk(); }
class A extends ParentA implements ITalk {}
class B extends ParentB implements ITalk {}

Notice how they have completely different parents but still implement the same interface? Now you could make a method that only cares if the object knows how to talk regardless of its parent:

void SomeMethod(ITalk talkingObject) { talkingObject.Talk(); }

And call it with instances of either type:

A a = new A();
B b = new B();
SomeMethod(a);
SomeMethod(b);

Basically it's a looser kind of inheritance, that only concerns itself with what the class has or can do but not how it does - which is why interfaces don't have implementations for their members.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.