• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class Object and Hierarchy

 
Joe Nguyen
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Opps Typo: ideally is the same as (3
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 805
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Joe Nguyen
Ranch Hand
Posts: 161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK. In the case I presented Vector extends Object and Bar extends Vector ( all single inheritance).
 
Kareem Qureshi
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joe,

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.

Thanks
Kareem
 
Paulo Lima
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
}


Regards,
Paulo Lima.
 
Stephen Bloch
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic