views:

154

answers:

5
class A implements Runnable  
class B extends A

Under these circumstances B IS A Runnable.

Is it valid to write:

class B extends A implements Runnable

If it is valid, will the run method in B override that of A? What could be the possible scenarios?
I'm uh confused...

A: 

Well, by extending A, you are already implementing Runnable, so you simply need to override the run() method and all will be well in Mordor.

If you don't, the superclasses run() will take effect.

Kyle Rozendo
+2  A: 

It is valid as interfaces only define that a method exists. B.run will override that of A, same as if B did not implement Runnable directly or if A did not implement Runnable but did have a public void run() method.

mlk
+5  A: 

Since "implements Runnable" doesn't introduce any executable code into a class but is basically just a promise to implement the necessary methods, repeating "implements Runnable" on a class that already extends another class that implements Runnable does effectively nothing.

There is a very slight difference that can be seen when using reflection, but other than that there is no difference.

Joachim Sauer
+2  A: 

Its enough to write

class B extends A

Class A already implements runnable so extending it makes this runnable too. To do something specific to class B override run() in class B.

Paul Whelan
+1  A: 
class B extends A implements Runnable

is valid Java syntax, but doesn't really do anything. By extending A, B is Runnable.

When the run method is called on an A object, A's run method will be called. When run is called on a B object, A's run method will be called unless B overrides A's run method

Consider the following:

class A implements Runnable
{
    public void run()
    {
          System.out.println("A's run method");
    }
}

class B extends A implements Runnable
{
    public void run()
    {
          System.out.println("B's run method");
    }
}

class Test
{
    public static void main(String args[])
    {
       A obj1 = new A();
       A obj2 = new B();
       B obj3 = new B();
       Runnable obj4 = new A();
       Runnable obj5 = new B();

       obj1.run(); // prints "A's run method"
       obj2.run(); // prints "B's run method"
       obj3.run(); // prints "B's run method"
       obj4.run(); // prints "A's run method"
       obj5.run(); // prints "B's run method"

    }
}
basszero
What if I do the following: Thread t1 = new Thread(new A()); Thread t2 = new Thread(new B()); t1.start(); t2.start();
Kevin Boyd
@Kevin: Exactly the same. Multi-threading has nothing to do with this.
Jesper
The same. The thread from t1 will print “A’s run method”, the thread from t2 will print “B’s run method”.
Bombe
@Bombe@JesperBut doesn't the run in B override the run in A, then both the runs should print "B's run method"
Kevin Boyd
Kevin - when you do "new A()", you've created an instance of A, not B.
weiji
This means when you do A.run(), it executes the code defined in the A class. B's overriding does not mean it replaces all the parent class' definitions for run().
weiji