Implementing interfaces and objects

When a class implements an interface, does it make objects created from the class perceived as an object of that interface?

i.e. Should a class that implements the Runnable interface create instances created from that class to call the Runnable object?

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

+3


source to share


5 answers


An object of a class C

that implements an interface I

can be called an object of that interface, although one object can be from multiple interfaces. Liskov's substitution principle requires to C

be used wherever it is required I

, so it essentially I

becomes a contract C

representing a subset of C

applicable to a particular situation.



For example, when an object implements Runnable

, a method run()

in an interface represents a specific aspect of a class in a Java class library, namely that objects of the class can be "run" (by calling run()

on them). Having Runnable

it allows you to code your thread logic independently of the Java developers who write their thread execution code independently of your implementation logic.

+5


source


A reference to Runnable is possible because the Runnable object must have all the methods in the Runnable interface.

This way you can access all of these methods at runtime.

If a class that is said to implement Runnable somehow fails to execute Runnable, there will be a compilation error, as the Java 7 Language Specification Chapter 8 (Classes) - 8.1.5 (Superinterfaces) defines:

A class is said to implement all of its superinterfaces.



The example given is as follows:

Example 8.1.5-3. Implementing superinterface methods

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

class Point { int x, y; };

class ColoredPoint extends Point implements Colorable {
    int color;
}

      

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

+2


source


If a class implements an interface, it can be used anywhere the interface type can be used. For example, if a class implements Runnable

, then an instance of that class can be used wherever it can be used Runnable

. This is an example of polymorphism.

For example, here's a class that implements Runnable

:

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

      

You can use MyRunner

like this:

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 class MyRunner

is called an instance Runnable

. You can even check this with 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, such as when you are instantiating a class through reflection.

+2


source


Objects inherit the interfaces of their parent classes, and methods from these interfaces can be overridden in subclasses.

The value of interfaces is that they allow you to create methods that inject multiple object classes into it, taking the input type of the interface.

Any object that implements an interface is actually an "interface" that can be used for the given interfaces that it implements.

Implementation of an interface by a class implies the ability of that class to execute any methods specified in the interface. Thus, any method that runs on an interface can run on something that implements the interface.

+1


source


The 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 checks this.

By convention, interfaces ending in "-th" are "accessible" to indicate this behavior (although this is by no means a hard and fast rule). But at the end of the day, the type of an object is java.lang.Object

either a direct / indirect extension of it. If you look at the inheritance tree in any Javadoc, you will see the class hierarchy extending each other and the known interface implementation classes.

Therefore, it is usually not said about the objects created by the class as interface objects, but rather that the class implements one.

0


source







All Articles