If every class in Java implicitly extends Object class and multiple inheritance is not possible in Java then how do we even extend any class?
-
Because single inheritancy is chained from one class to the next. Multiple inheritancy deals with extending from two or more objects within a single generationMadProgrammer– MadProgrammer2013-04-11 06:01:36 +00:00Commented Apr 11, 2013 at 6:01
-
1Read about difference between multiple and multi level inheritance.prashant– prashant2013-04-11 06:03:58 +00:00Commented Apr 11, 2013 at 6:03
-
stackoverflow.com/questions/13926555/… and stackoverflow.com/questions/7553201/…Habib– Habib2013-04-11 06:12:57 +00:00Commented Apr 11, 2013 at 6:12
-
@Habib I don't have any confusion regarding Multiple or Multi-level inheritance. Anyways got my doubt cleared.ThanxBhushan– Bhushan2013-04-11 06:24:51 +00:00Commented Apr 11, 2013 at 6:24
5 Answers
"Java doesn't have multiple inheritance" means that you can't have two different parents, not that your parent can't have a parent.
C++ is an example of a language that lets you do multiple inheritance: http://www.learncpp.com/cpp-tutorial/117-multiple-inheritance/
Multiple inheritance looks like this:
class Teacher: public Person, public Employee
which means 'Teacher extends Person and Employee, inheriting its fields and methods'.
Instead of multiple inheritance, you're expected to create and implement interfaces to represent all the behaviours (or contracts if you prefer) your object supports. Java uses this for interfaces like Closeable and Serializable.
3 Comments
Multiple Inheritance is the concept of a single class inheriting from two or more super classes. If a class inherits from a super class and if that super class inherits from another super class it does NOT qualify as Multiple Inheritance. It is still Single Inheritance.
once u create object of sub class the object hierachy would be create on order of
objectclaass–>superclass—>subclass;
It is true that every class in Java inherits from the Object class – either indirectly or directly.
so in this case the sub class indirectly inherits object class.
Comments
Every Class that dosn't extends any other class it extends Object class. if you extends anther class example extends Vector class look to the hierarchy of class Vector you will end with a simple class that dosn't extends any anther class which explicitly extends Object. and Any class extends anther class it explicitly extends all class that the parent class extends.