my dog learned polymorphism
The moose likes Beginning Java and the fly likes can a class extend two classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "can a class extend two classes" Watch "can a class extend two classes" New topic

can a class extend two classes

Sarat Chandra

Joined: May 19, 2006
Posts: 1
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

Joined: Sep 16, 2005
Posts: 1780
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 ]

There is no emoticon for what I am feeling!
Tony Morris
Ranch Hand

Joined: Sep 24, 2003
Posts: 1608
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.

Tony Morris
Java Q&A (FAQ, Trivia)
I agree. Here's the link:
subject: can a class extend two classes
It's not a secret anymore!