0

Well Java does not support multiple inheritance in java.. But wait in eclipse we can see any class extends OBJECT class by default, we can see all methods of Object class if we try to add unimplemented methods.

Now MY POINT IS i can make my class extend any class for example Thread. So now my class extends Thread by user defined side and Object by default... that means multiple class inheritance ?

Denny Mathew
  • 842
  • 3
  • 13
  • 31
  • 8
    Nope. `YourClass <- Thread <- Object`. Single inheritance (which just means a class can only have one *direct* super-class). – user2864740 Nov 05 '13 at 06:53
  • 1
    The `Object` class is always at top of the inheritance hierarchy, in this situation creating a chain of classes - multilevel inheritance. – Lion Nov 05 '13 at 06:56
  • 1. You cannot extend 'final' classes in Java. 2. I would advise checking the definition of multiple inheritance in languages like C++. – Alfred Xiao Nov 05 '13 at 07:07
  • 1
    There are so many versions of this question [1](http://stackoverflow.com/q/17897244/1037210), [2](http://stackoverflow.com/q/17505030/1037210), [3](http://stackoverflow.com/q/8470535/1037210), [4](http://stackoverflow.com/q/4452461/1037210)... – Lion Nov 05 '13 at 07:10
  • 1
    So everyone is saying,,,if i dont use extends my class will inherit Object class directly,,else if i refer Thread class it will leave ,and maintain the prior definition via Threads to Object... ryt! – Denny Mathew Nov 05 '13 at 07:12

5 Answers5

1

Behavior similar to multiple inheritance can be seen with Java interfaces:

// implements BOTH Runnable AND ActionListener

public class MultipleInterfaces implements Runnable, ActionListener {
    @Override public void run() {}
    @Override public void actionPerformed(ActionEvent e) {}
}

Multiple inheritance would be like this:

// Not allowed, complete nuts

public class Amalgam extends ArrayList<Thread>, JPanel, Font {
    public Amalgam() {
        super(); // <- and what would this do?
    }
}
Radiodef
  • 37,180
  • 14
  • 90
  • 125
  • Yeah thanks for the info. Since you mentioned Runnable. Leme ask why does Thread class implements Runnable. Isnt Thread supposed to be having its own definition for run ? sorry if the Q was lame :) – Denny Mathew Nov 05 '13 at 07:16
  • 1
    It is actually a reasonable question. The way it's supposed to be, Thread is itself a runnable that runs a runnable that you hand in to the constructor. `new Thread(new Runnable() @Override public void run() {}).start();` See _"Defining and Starting a Thread"_: http://docs.oracle.com/javase/tutorial/essential/concurrency/runthread.html The other way you can look at it is Thread is a runnable with some extra features. You can extend Thread and override `run`. But technically it is "better" to do it the first way. – Radiodef Nov 05 '13 at 07:23
1

A class can only have one superclass i.e. in java one class can only extend one class. If one is not specified, then it is implicitly extends to Object.

So suppose the class is MyClass and it extends MySuperClass. As MyClass extends MySuperClass so it will not extend directly Object. But MySuperClass class itself is not explicitly extending any class so it extends Object and in turn MyClass also extends Object in the hierarchy.

So it is not Multiple inheritance rather it is Multilevel inheritance. Hope it helps.

Trying
  • 14,004
  • 9
  • 70
  • 110
0

The behavior mentioned by you is multi-level inheritance which java supports by default

Karthik
  • 1,005
  • 8
  • 7
0

This is MultiLevel inheritance and not Multiple inheritance.

Dark Knight
  • 8,218
  • 4
  • 39
  • 58
0

any class extends OBJECT class by default

That means Thead class extends Object too.

PKopachevsky
  • 262
  • 1
  • 6