8 Replies - 423 Views - Last Post: 27 January 2011 - 07:36 PM Rate Topic: -----

#1 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1618
  • View blog
  • Posts: 5,707
  • Joined: 03-August 09

interfaces

Posted 27 January 2011 - 02:41 PM

ok so a concept im not used to from C++ is interfaces. dose the class witch implements an interface have to implement the interface exactly or can it implement a comparable function?

for example

public interface Cmp {
    int cmp(Object x);
}



can the member
int cmp(AnyClass x)
implement the inter face Cmp? 'AnyClass' is just that some class because all classes inherent form Object.

This post has been edited by ishkabible: 27 January 2011 - 02:42 PM


Is This A Good Question/Topic? 0
  • +

Replies To: interfaces

#2 moobler  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 143
  • View blog
  • Posts: 224
  • Joined: 21-January 11

Re: interfaces

Posted 27 January 2011 - 02:53 PM

The inheriting class must implement all methods with the exact headers defined in the interface.
Was This Post Helpful? 1
  • +
  • -

#3 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1618
  • View blog
  • Posts: 5,707
  • Joined: 03-August 09

Re: interfaces

Posted 27 January 2011 - 02:54 PM

kk thanks!! when i get home ill play around with it :)
Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2861
  • View blog
  • Posts: 10,968
  • Joined: 15-July 08

Re: interfaces

Posted 27 January 2011 - 02:56 PM

Interfaces are a way to require a class to meet a certain expectation in the methods of that class. Since Java only allows you to extend one class directly, this is the way that you can force a class to conform to other standards too. However, the key difference is that a class that implements an interface does not receive an implemented version of that. The class has to implement it itself. This allows for some complex behavior that isn't possible with single inhabitance. For example, say we want to invoke an actionPerformed method on anything in our GUI that has an ActionListener interface.

Lets assume our class isgnature looks like this:
class MyComponent extends JComponent implements ActionListener



You COULD code it like this:
MyComponent[] j = new MyComponent[10];
//...fill up the array
// loop through and invoke it.



However, what if you then made a subclass of that component? Are you SURE that that class implements ActionListener? Not all the time in software will you know what a superclass inherits and implements.

You're also limiting it to JComponents...instead of making an array of Objects, which don't implement ActionListener, you can simply do this:
ActionListener[] alArr = new ActionListener[10];



Now, you are not limited to a certain class and limit it to whether or not they have implemented actionPerformed(). ANY object that implements it can go into this array, which is easier than trying to find a common ancestor that does implement that method.
Was This Post Helpful? 2
  • +
  • -

#5 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1618
  • View blog
  • Posts: 5,707
  • Joined: 03-August 09

Re: interfaces

Posted 27 January 2011 - 03:14 PM

wow that's really nice i wish C++ could do that (C++ would have alignment issues), you would have to cast each element as you accessed them to work with it right?

This post has been edited by ishkabible: 27 January 2011 - 03:15 PM

Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2861
  • View blog
  • Posts: 10,968
  • Joined: 15-July 08

Re: interfaces

Posted 27 January 2011 - 03:18 PM

To call the actionPerformed() method (which is implemented in any class that implements ActionPerformed), there is no casting if you use it the second way, with the arrays of ActionListeners. That's the cool part about it - no need for casting...

Edit: WHOOPIE 1500 rep!
Was This Post Helpful? 0
  • +
  • -

#7 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: interfaces

Posted 27 January 2011 - 03:51 PM

I have written a tutorial about abstract classes vs. interfaces in Java:
http://www.dreaminco...-vs-interfaces/
Was This Post Helpful? 1
  • +
  • -

#8 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2861
  • View blog
  • Posts: 10,968
  • Joined: 15-July 08

Re: interfaces

Posted 27 January 2011 - 03:57 PM

View Postjapanir, on 27 January 2011 - 05:51 PM, said:

I have written a tutorial about abstract classes vs. interfaces in Java:
http://www.dreaminco...-vs-interfaces/


I'll need to remember that one...
Was This Post Helpful? 0
  • +
  • -

#9 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1618
  • View blog
  • Posts: 5,707
  • Joined: 03-August 09

Re: interfaces

Posted 27 January 2011 - 07:36 PM

wow, im beginning to be more impressed with Java, that's borderline dynamic typing right there. i need to use java more and find out all the goody's I've been missing.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1