Interfaces

what is a good way to use one

Page 1 of 1

6 Replies - 1057 Views - Last Post: 17 April 2009 - 04:34 PM Rate Topic: -----

#1 virgul   User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 269
  • Joined: 18-March 09

Interfaces

Posted 16 April 2009 - 10:53 PM

So for my end of the year project we are asked to include a bunch of things, including an Interface. I personally cant think of a good way to use one, i have no idea what there really even useful for.

Can someone give me some clue what an interface can be used for.
Can an interface be used to pass around variables?

Anyway my project is a turn based paintball like game.



I didnt put this in the homework section because im not asking for code, but if it belongs there feel free to move this thread.


O, and one more question, is the Runnable class an interface?

This post has been edited by virgul: 16 April 2009 - 10:54 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Interfaces

#2 Fuzzyness   User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: Interfaces

Posted 16 April 2009 - 10:57 PM

Well, you could look at tutorials on Interfaces in general. You cannot passdown a variable unless it is in the parameter of a method. Basically an interface is a list of methods that are related with empty body. However, if your class implements an interface, all methods must appear in the code. For example, say you have a bike class and have an interface for bicycles. would look something like this
interface Bicycle {

	   void changeCadence(int newValue);

	   void changeGear(int newValue);

	   void speedUp(int increment);

	   void applyBrakes(int decrement);
}


Hope this helps and good luck!
Was This Post Helpful? 1
  • +
  • -

#3 virgul   User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 269
  • Joined: 18-March 09

Re: Interfaces

Posted 16 April 2009 - 11:00 PM

wow, thanks for the quick reply! One question though, whats the difference between that and an Abstract?

To me it seems like i can get the same thing done with and abstract, but the abstract allows for the body to be defined.
Was This Post Helpful? 0
  • +
  • -

#4 hello1212   User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 80
  • Joined: 29-December 08

Re: Interfaces

Posted 17 April 2009 - 01:38 AM

there are many diifferernce
1)A abstract class can have fields and constructors. An interface cannot have fields (except, possibly, some public static final constants){ONLY ABSTRACT METHODS}not even constructors

2)REmember:THE MAIN ADVANTAGEAND THE WHOLE ADVANTAGE OF INTERFACES IS THAT A CLASS CAN EXTEND ONLY ONE ABSTRACY CLASS WHEREAS A CLASS CAN IMPLEMENT ANY NUMBER OF INTERFACES
Was This Post Helpful? 1
  • +
  • -

#5 BigAnt   User is offline

  • May Your Swords Stay Sharp
  • member icon

Reputation: 102
  • View blog
  • Posts: 2,392
  • Joined: 16-August 08

Re: Interfaces

Posted 17 April 2009 - 07:46 AM

The main purpose of an interface is not to lock down your code to using a concrete implementation of a class.

As an example with the List interface, This Lists all the methods that a List needs to implement. Now there can be multiple ways to implement Lists, but if they all implement the same interface you get many advantages, such as dynamically changing your list type a runtime.

Exampe: Say you are having a class uisng an ArrayList like: ArrayList myL = new ArrayList();

And you decide to have the user choose the type of list they want when they run the program, you will have to have fields for each and every type of list that are options.

But lets says they all implement the List interface. You can now declare like:
List myList;
And when they choose their type of list you can just instantiate that type of list as:
myList = new ArrayList(); or myList = new LinkedList();

In the first example using the concrete implementation you have to redo the declaration if you want to change the List type, but with the second interface declaration you just change what type you instantiate the List to.

This is just one example but there are many more.
Was This Post Helpful? 1
  • +
  • -

#6 c0mrade   User is offline

  • D.I.C Regular

Reputation: 20
  • View blog
  • Posts: 412
  • Joined: 16-November 07

Re: Interfaces

Posted 17 April 2009 - 02:47 PM

^ ^ What BigAnt said.

It's actually a pretty hard thing to describe...

This post has been edited by c0mrade: 17 April 2009 - 02:47 PM

Was This Post Helpful? 0
  • +
  • -

#7 virgul   User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 269
  • Joined: 18-March 09

Re: Interfaces

Posted 17 April 2009 - 04:34 PM

thanks everyone, i really do understand what i can use them for, unlike the examples from the book im using...

thanks again!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1