您的位置:寻梦网首页编程乐园Java天地Core JavaJava Lecture Notes
Introduction
Content
Apply
Reflect
Extend
previous.gif
 (3087 bytes)
next.gif
 (2959 bytes)

 
 
Content Index
Content Page # 7

Multiple inheritance

  • Multiple inheritance embodies the idea that one thing is a type of more than one other thing 
  • True multiple inheritance is not handled in Java 
  • Java supports something called `lightweight' multiple inheritance
In the previous example, Circle was a type of Shape , which was itself a type of Moveable . These classes form a hierarchy with Moveable at the `top' and Circle at the `bottom'. Circle was only indirectly a type of Moveable. Suppose we want to state that a class is a direct subclass of two others. Here is an example. Suppose any object that can receive input from the user is a subclass of KeyboardListener . Examples might include buttons, menus, window borders, etc. Suppose

any object that can be displayed in a window is a subclass of Component (this is a real Java entity, and will be described in more detail later). 

Suppose we now define a class called MyButton which displays a button in a window. This is a subclass of KeyboardListener (because we can activate the button by pressing the space bar, or clicking the mouse). It is a subclass of Component because it can be displayed in a window. But Component is not a subclass of KeyboardListener . Why? Because there may be other classes that can be displayed in a window, but cannot receive input (e.g., an icon, or an image). So these classes do not form a hierarchy. MyButton is a subclass of both KeyboardListener and Component. 

You might think we could describe this in Java using something like: 

class MyButton extends Component, KeyboardListener
{
  // methods go here
 }
But this won't work. Java does not support multiple inheritance . The debate about whether multiple inheritance should be allowed in programming has been going on for a long time, and is not yet settled. Whether it is a good thing or a bad, the Java developers decided not to allow multiple inheritance in Java. You will have an opportunity to make your own mind up about the correctness of this decision later.

Java does, however, support something called ' lightweight' multiple inheritance , using interfaces

Back to top

basicline.gif (169 bytes)

RITSEC - Global Campus
Copyright © 1999 RITSEC- Middlesex University. All rights reserved.
webmaster@globalcampus.com.eg