Tuesday 4 December 2012

Java doesn't support multiple inheritance but implicitly every class in java extends Object and allows one more

1 comment
Your class that extends that other class, but it extends Object, too, so you're still in one line of inheritance, not two.


No. In Java, a class can only inherit from one class and by default this is the Object class that you refer to. We can however specify a different class to inherit from (using the 'extends' keyword). However, this parent class will itself have a parent class and so on until ultimately we get back to the Object class. Perhaps an example will help:
class Animal {
}

class Cat extends Animal {
}

class Tiger extends Cat {
}
In the above example Tiger inherits from Cat which inherits from Animal which (by default) inherits from Object.
Hope this clears things up a touch.

1 comment:

  1. Very good to know - thanks!

    Another good one:

    http://www.programmerinterview.com/index.php/java-questions/multiple-inheritance/

    ReplyDelete