Not logged in, Join Here! or Log In Below:  
News Articles Search    

 Home / General Programming / Interfaces in Java Account Manager
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.

February 18, 2005, 10:33 AM

hey there chaps, the time has come for me to once again ask you all a coding question! here goes:

i'm currently doing a group project for my uni course, which involves taking a datafile with information about a race (when cars pass certain checkpoints, retire, take a pit-stop, etc), processes this, and can display the race in real-time. this is just to set the project in context, my actual question is about interfaces.

so far, we have several interfaces, which include the basic core public methods that other classes will be able to call. for instance, there's an interface ICar, which has things like getSpeed() and so on. we then have classes that implement these interfaces- in the same example, we have class Car, which implents ICar- it specifies what getSpeed() and others do, as well as having some extra private functions and variables. throughout the project, it's one class to one interface, no two classes implement the same interface.

so anyway, all is going well- we have a base code which we need to implent with other classes- until one team member throws a question at me i can't answer. why do we need interfaces?

so i sat and thought about it... nothing else uses ICar, except for Car. for instance, in our Race class, which stores information about the race, we have an array of Cars, but no reference to ICar anywhere... it's as if the interface is only there to ensure we implement certain methods.

the whole idea of interfaces, or so i thought, was to provide other classes with the core functionality of a class, which could then later be implemented, but as far as i can see, it would work precisely the same without the interface, and the word 'implements ICar' missing from the Car implementation.

well, i went looking for some examples for how to actually use interfaces, but all of the examples i could find were simple, one interface, one class examples- no examples of a class needing to use another class which implements an interface. either that, or they're just examples of how to cheat multiple inheritance in java.

anyways, the question i'm trying to ask is: what is the real purpose of java interfaces?

and also, can anyone give me an example of how they can be used in the way i mentioned- multiple classes that may contain instances (or references to) each other, working together through interfaces.

if someone could help, i would be truly appreciative. thank you very much, folks :)


February 18, 2005, 11:08 AM

If you are in a real university why don't you go to the library and read a couple of books on Java instead of wasting everyones time? I find it humerous that someone is trying to develop a graphics app without a basic understanding in the language used to implement the app.


February 18, 2005, 11:21 AM

thank you for your informative and helpful reply, 'sp0rtyt'. my problems are solved! a book!!! now why didn't i think of that?!?

oh yes, i did. the books, as i say, feature very simple examples, nothing like what i'm doing. believe me, i wouldn't post here without having looked thoroughly myself first, which i have.

the problem is, they only teach us 'basic' java in the first place, as you would know if you were doing my course. but you're not, so don't go making assumptions about it's content, and the available resources. the course is not about coding, and familiarity with a particular language, which is why i feel the need to ask questions. the only reason i've wasted your time is because you've replied.

now, if anyone's got a real reply and would actually like to help me out, please do so! thanks.

Alex Herz

February 18, 2005, 12:14 PM

simple real live example: input devices

User could steer a car by mouse/joystick/keybpard/joypad/...

Now somewhere in your main loop you could put a big case statement that checks what is the current input device like this:

public Vec2d Input::GetInput()
1: return Mouse.GetDirection();
2: return Joystick.GetDirection();

Obviously, mInput and the respective input device ("Mouse","Joystick",..)
need to be initialized.

The nicer way to do this is to use an interface. An interface basically defines how to interact with a black box device. You know how to use the device (using the interface functions) but you don't know what happens internally:

interface InputDevice
public Vec2d GetDirection();

class Joystick implements InputDevice
public Vec2d GetDirection()
//do something to querry joystick and calc a 2d dir vec

class Mouse implements InputDevice
public Vec2d GetDirection()
//do something to querry mouse and calc a 2d dir vec

now somewhere in your code you create either a "Mouse" object or "Joystick" object, depending on what the user has choosen to use. You pass this object to your main loop and stoore it in an InputDevice type variable. Now you can use the generic InputDevice object to interact with the input device choosen by the user without having any trouble with it:

InputDevice in_dev=object; //object is of type mouse or joystick or anything else that inplements InputDevice

Vec2d dir=in_dev.GetDirection();

So no more ugly case statement.

Looking at your car problem I'd say it MIGHT make sense to use interfaces in some places. You corrctely asserted that it doesn't make sense if you have only one class per interface (apart from the fact that the interface is a bit more readable than the class definition).

In addition to the abstraction that interfaces provide (as shown with input device) properly defined interfaces can avoid ppl messing with/hacking your class definitions. In big projects at late night ppl often get messy and break a classes definition, they make something public though it shouldn't be etc.. interfaces make it a bit harder to to this.



February 18, 2005, 12:27 PM

It's really just a simple thing which you already understand:

"... it's as if the interface is only there to ensure we implement certain methods."

That's the meat of the whole interface thing. In the real world, when people are writing code to interact with other people's code, they need assurances that objects can do certain things. Like how a car needs to report its speed with getSpeed(). Interfaces provide that assurance, without any class heirarchy crap when it isn't necessary. So if a programmer writes his own Car class with the ICar interface, someone else will know that certain things can be done with it. Simple.


February 18, 2005, 01:26 PM



February 18, 2005, 03:27 PM

DonJuan pretty much says it...
I'd like to add that interfaces also replace most of the functionality that function pointers and multiple inheritance give you in a language like C++.
By implementing an interface, you 'assure' Java that certain functions exist... You can cast an object to its interface, and pass it around... and call the functions of that interface... so the object reference has more or less become the equivalent of the function pointer to the functions of the interface.

And although you can only inherit one class, you can implement as many interfaces as you like in a new class. It's a much cleaner and safer solution than multiple inheritance. I've never come across a situation where I couldn't handle the problem without multiple inheritance, in the years I've developed Java.


February 19, 2005, 06:40 AM

indeed, i would, but i need to convince my teammates over the weekend that interfaces are the way to go, else they will start programming without them, since we have a rather limited time in which to do this. contacting any of the academic staff when they're not working is nigh-on impossible.


February 19, 2005, 06:48 AM

i see, thank you all for your replies :)

well then, i guess i will use interfaces- not to allow multiple classes to inherit from the interface, more just to force people to write the code that i want them to... not all of my team can program particularly well (not that i can, either), and i don't want them breaking the classes. the method we were on the verge of following was to write out skeleton code for each person to fill out, which is quite prone to breakage!

thank you all again, you've been very helpful!

Andrew Walker

February 19, 2005, 06:30 PM

Perhaps a bit late but:

Interfaces are a good method of following the

This thread contains 10 messages.
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.