aspose file tools*
The moose likes Beginning Java and the fly likes Class Object and Hierarchy Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Class Object and Hierarchy" Watch "Class Object and Hierarchy" New topic

Class Object and Hierarchy

Joe Nguyen
Ranch Hand

Joined: Apr 20, 2001
Posts: 161
According to java API Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class. Java does not permit multiple inheritance, but it is not the case.
For instance:
(1) Class Foo extends Object{} //no compiling error
(2) Class Bar extends Vector{} // ideally is the same as (2) but with no compiling error
(3) Class Foo extends Object, Vector // copiling error

Does it mean java internally does permit multiple inheritance?
Joe Nguyen
Ranch Hand

Joined: Apr 20, 2001
Posts: 161
Opps Typo: ideally is the same as (3
Steven Bell
Ranch Hand

Joined: Dec 29, 2004
Posts: 1071
I'm not sure what you are getting at. Multiple inheritance is when you subclass two distinct classes. Example:

This is different from having a hierarchy of parents. Example:

[ March 29, 2005: Message edited by: Steven Bell ]
Jeff Bosch
Ranch Hand

Joined: Jul 30, 2003
Posts: 805
The only place Java permits "multiple inheritance" is of types, and that's done through interfaces. So, while a class can inherit directly from only one class (which in turn inherits from its superclass and on up to Object), a class can implement multiple interfaces. This is one of the foundations of polymorphism in Java...

Give a man a fish, he'll eat for one day. Teach a man to fish, he'll drink all your beer.
Cheers, Jeff (SCJP 1.4, SCJD in progress, if you can call that progress...)
Joe Nguyen
Ranch Hand

Joined: Apr 20, 2001
Posts: 161
OK. In the case I presented Vector extends Object and Bar extends Vector ( all single inheritance).
Kareem Qureshi
Ranch Hand

Joined: Mar 14, 2002
Posts: 102

Java implements what is known as a single-inheritance model.

This means that when someclass is not extending from any other superclass then it is extending from Object class by default, but if it is extending from a superclass which is in turn extending from Object class by default. That means someclass is also extending from Object class

I would want to see what others think.

Paulo Lima

Joined: Apr 04, 2002
Posts: 10
Dear all,

Java allows multiple inheritances of Interfaces. Also one class can implements several interfaces.

This code is possible in Java:

interface InterfaceA {
public void methodA();

interface InterfaceB {
public void methodB();

interface InterfaceC {
public void methodC();

interface InterfaceD extends InterfaceA, InterfaceB, InterfaceC {
// multiple inheritance in Java only for Interfaces !!!
public void methodD();

interface InterfaceE {
public void methodE();

public class Test implements InterfaceD, InterfaceE {
// multiple inheritance in Jave for one class that implements multiple Interfaces
public void methodA() { System.out.println("InterfaceA contract from InterfaceD"); }; // InterfaceA contract from InterfaceD
public void methodB() { System.out.println("InterfaceB contract from InterfaceD"); }; // InterfaceB contract from InterfaceD
public void methodC() { System.out.println("InterfaceC contract from InterfaceD"); }; // InterfaceB contract from InterfaceD
public void methodD() { System.out.println("InterfaceD contract from InterfaceD"); }; // InterfaceD contract from InterfaceD
public void methodE() { System.out.println("InterfaceE contract from InterfaceE"); }; // InterfaceE contract from InterfaceE

Paulo Lima.
Stephen Bloch
Ranch Hand

Joined: Aug 19, 2003
Posts: 48
There are, BTW, good reasons that Java doesn't allow multiple inheritance. For example, consider

If you call whatDoesThisDo() on a Boojum object, which version of x does it increment? Which version of y does it decrement? Which version of doSomething() does it call? It's not clear what the program is supposed to do, much less how to make the compiler and JVM do that.

In C++, there are obscure rules to control which of these things happens, and you can change which one happens by using the keyword "virtual" (yet another of the half-dozen meanings of that keyword in C++). The designers of Java decided to avoid all those problems by just not allowing multiple inheritance. It turns out that to avoid all these vexing issues, it suffices to make sure no class inherits from two different parents that have instance variables, and no class inherits from two different parents that have method implementations; these are exactly the constraints imposed on the Java "interface" construct, so we can inherit from multiple interfaces but only one class.

That said, there are situations in which multiple inheritance would be nice. For example, I like to introduce linked lists in the classroom via polymorphism, rather than with an "if (current == null)" statement. (For details, see my paper on the subject.) When I move on to doubly-linked lists, I may write

It would save some trouble, and avoid some duplicate code, if HasSuccessor and HasPredecessor could have instance variables and default implementations of certain methods. But they can't, in Java. Arguably a small sacrifice for avoiding all the headaches of multiple inheritance.
[ March 29, 2005: Message edited by: Stephen Bloch ]

SCJP 1.4
wood burning stoves
subject: Class Object and Hierarchy