Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can a class extend two classes

 
Sarat Chandra
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i know that a class can extend only 1 class. But it is said that every class has object class as its super class . So it says that if a class A extends class b and also class object.
is it true. is it a valid doubt.
please give me a idea wat exactly is happening.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What makes a "doubt" a "valid doubt"? I think you need to read about single inheritance versus mulitple inheritance and the basic notion of a class hierarchy. Java has single class inheritance. When you define a class you can write

class B extends X {...}

but not

class B extends X, Y {...} //as one can do in C++, for example.

Because you can continue this process and define a class to extend B:

class C extends B {...}

a hierachy may result. Consider javax.swing.JButton:

JButton extends AbstractButton
AbstractButton extends JComponent
JComponent extends Container
Container extends Component
Component extends Object

Because at each step the newly defined class can only *directly* extend one class, this is not multiple inheritance.
[ May 19, 2006: Message edited by: Jeff Albertson ]
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All classes inherit one and only one class - with the single exception of java.lang.Object - which does not inherit from any class. All classes inherit directly (implicitly or explicitly) or indirectly from java.lang.Object - again with the exception of java.lang.Object itself.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic