-1

Possible Duplicate:
What is an interface in Java?

I have experience with object languages and object paradigm, but I have one doubt about "implements" statement. Doubt is does it includes code (functionality)? So to be very specific Does "MyClass implements XInterface" include code from other classes that implement that interface?

Class inheriting is simple including code, in a way, I know "including" may not be right word but that is what is talking place when you use "extends". You include functionality from parent class.

So let use Runnable interface for example. It has only one run() deceleration. Which could mean nothing, just that MyClass will have implementation of run(). And that what confuses me, like implementing one line deceleration in some file will make me some good... where is the actual functionality (and code) that I will get by using "implements"? So if I use:

MyClass implements Runnable {}

Runnable obj = new (Runnable) MyClass();

obj.run();//will call run() implementation from MyClass
obj.otherFunctions();/* so this calls functions from other classes that implement interface, but I don't know what functions from other classes are implementing, or even do I need them?*/

Also do I have to have one class per thread, like MyClass extends Thread implements Runnable ?

Community
  • 1
  • 1
ratko ratko
  • 105
  • 1
  • 7
  • 4
    If a class "implements" an interface, it must implement that interface. – Dave Newton Dec 09 '12 at 22:53
  • MyClass implements Runnable {} won't compile: you must have all methods of the interface implemented in the class. – full.stack.ex Dec 09 '12 at 22:54
  • 1
    http://docs.oracle.com/javase/tutorial/java/concepts/interface.html - it would benefit you to go through the tutorials. Your question makes little sense in regard to "other objects". And Interface is a *contract* that means your class will provide those methods – Brian Roach Dec 09 '12 at 22:54
  • So it guaranties that class will implement **ALL** interface methods (or will be abstract, and still wont be able to be instanced). And what is relation between MyClass that implements InterfaceX and SomeOtherClass implements InterfaceX? None? – ratko ratko Dec 09 '12 at 23:55

1 Answers1

2

A class Cl that implements an Interface means that all methods that are defined in the interface must be implemented in class Cl. The calling class then can call these methods, because it is known that Cl implements (fullfills) the interface.

For example see the List interface: ArrayList and LinkedList both implement List, they have the same public methods (e.g add(), get()), but are internally working different.

List list = new ArrayList();
list.add(3);

later if somebody wants ArrayList to be exchanged by another class, that also implements List, only one line has to be exchanged

List list = new LinkedList();
list.add(3);

If programming to Interfaces the code is less dependent on a specific class. this has advantages when you later want to exchange one class for e,g purpose of testing, where you could controll that class by a test case.

AlexWien
  • 28,470
  • 6
  • 53
  • 83