3

When a class implements an interface, does that make objects instantiated from the class be perceived as an object of that Interface?

i.e. Upon a class implementing the Runnable interface, does that make instances created from that class to be called a Runnable object?

So, where a Runnable object reference variable is expected (say, in the parameter of a method or a constructor), why is it legal that we can provide an instance of the class as an argument to that method or constructor? Is it because by implementing the interface, the class, is in essence, an object of the Interface?

151SoBad
  • 115
  • 9

5 Answers5

5

An object of a class C that implements an interface I can be called an object of that interface, although a single object can be of many interfaces. Liskov substitution principle requires C to be usable anywhere where I is required, so in essence I becomes a contract of C, representing a subset of Cs abilities, as applicable to a certain situation.

For example, when an object implements Runnable, the run() method in the interface presents a particular aspect of the class to Java class library - namely, that objects of the class can be "ran" (by calling run() on them). The presence of Runnable lets you code the logic of your thread independently of Java designers, who write their thread-execution code independently of your implementation's logic.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Thank you. So just to confirm, where an Interface is expected as an argument to a method, if a particular class implements the interface within the class body, then you can pass the keyword 'this' as the argument to the method? That is, this only works because 'this' (which refers to the object in which the method was invoked) **contains** the implemented method of the Interface? – 151SoBad Apr 25 '15 at 06:47
  • @151SoBad "[...] you can pass the keyword `this` as the argument to the method?" This is correct. "this only works because `this` [...] contains the implemented method of the Interface?" A class can contain all methods from an interface without implementing that interface (e.g. because the author forgot to write `implements XyzInterface`). In this case having all the methods would not be sufficient: one needs to declare the class as an implementation of an interface, which comes with the promise of implementing all interface methods. – Sergey Kalinichenko Apr 25 '15 at 09:42
2

The reference to a Runnable is possible since the Runnable object must have all methods in the Runnable interface implemented.

This way you can access all those methods at run time.

If the class that is said to implement Runnable would somehow not implement Runnable - there would be a compilation error, as Java Language Specification 7 chapter 8 (classes) - 8.1.5 (superinterfaces) specifies:

A class is said to implement all its superinterfaces.

The example given is the following:

Example 8.1.5-3. Implementing Methods of a Superinterface

interface Colorable {
    void setColor(int color);
    int getColor();
}

class Point { int x, y; };

class ColoredPoint extends Point implements Colorable {
    int color;
}

This program causes a compile-time error, because ColoredPoint is not an abstract class but fails to provide an implementation of methods setColor and getColor of the interface Colorable.

Reut Sharabani
  • 30,449
  • 6
  • 70
  • 88
2

If a class implements an interface, it can be used in any place where the interface type can be used. For example, if an class implements Runnable, then an instance of that class can be used anywhere where a Runnable can be used. This is an example of polymorphism.

For example, here is a class that implements Runnable:

public class MyRunner implements Runnable {
  public void run() {}
}

You can use MyRunner like follows:

MyRunner runner = new MyRunner();

// can assign to a field of type "Runnable" without a cast
Runnable runnable = runner;

// can pass to methods that take a Runnable
Executors.newFixedThreadPool(3).execute(runner); 

The MyRunner class is said to be an instance of Runnable. You even can check this via reflection;

public void runIfIsRunnable(Object object) {
  if (object instanceof Runnable) {
    Runnable r = (Runnable) object;
    r.run();
  }
}

Using instanceof is often considered a code smell, but there are situations where it is useful, like when you create instances of a class via reflection.

NamshubWriter
  • 23,549
  • 2
  • 41
  • 59
1

Objects inherit their parent classes interfaces, and methods from those interfaces can be overridden in sub classes.

The Value of interfaces is that they allow for methods to be created that accommodate a variety of object classes as input, by accepting the interface type as input.

Any object that implements an interface is in fact "interface"able for the given interfaces it implements.

The implementation of an interface by a class implies the ability of that class to perform any methods specified within the interface. As such any method that runs on the interface can run on something implementing the interface.

kpie
  • 9,588
  • 5
  • 28
  • 50
0

An interface establishes a contract that the object will contain the methods defined in the interface and this contract is enforced by the compiler. So the compiler will check for this.

By convention, interfaces end in "-ible" are "-able" to show this behavior (though this by no means is a hard rule). But in the end, the type of the object is either java.lang.Object or a direct/indirect extension of it. If you look at the inheritance tree on any Javadoc, you'll see the hierarchy of classes extending one another and known implementing classes of interfaces.

So one doesn't normally speak of objects created by the class as objects of an interface, but rather that the class implements one.

riddle_me_this
  • 8,575
  • 10
  • 55
  • 80