1
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...

1
  • 1
    This question is not about multithreading. Runnable instances don't have to run in separate threads. Commented Sep 9, 2009 at 11:45

5 Answers 5

6

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

2

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.

Comments

1
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"

    }
}

6 Comments

What if I do the following: Thread t1 = new Thread(new A()); Thread t2 = new Thread(new B()); t1.start(); t2.start();
@Kevin: Exactly the same. Multi-threading has nothing to do with this.
The same. The thread from t1 will print “A’s run method”, the thread from t2 will print “B’s run method”.
@Bombe @Jesper But doesn't the run in B override the run in A, then both the runs should print "B's run method"
Kevin - when you do "new A()", you've created an instance of A, not B.
|
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.