This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Java in General and the fly likes Does java extend 2 classes at one time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » Java in General
Bookmark "Does java extend 2 classes at one time" Watch "Does java extend 2 classes at one time" New topic

Does java extend 2 classes at one time

raghavan kanagiri

Joined: Feb 07, 2014
Posts: 1
1) when you create a class and compile it and open and see the class using javap command .. that class file contains that it extends java.lang.object and a default constructor is created automatically..... But when you extends another class the java.lang.Object class is not appeared as extended why?

Code example:

1St Case ----- Java code:

public class temp(){


Javap Code:

public class temp() extends java.lang.Object {
public temp(){


2nd case --- Java Code:

public class temp() extends dummy{


javap Code:

public class temp() extends dummy{
public temp(){

why for the above scenario it doesn't extends object class .. if it does implicitly then why it did not do in the first case instead why did the compiler extends Object class ?
Jeanne Boyarsky
author & internet detective

Joined: May 26, 2003
Posts: 32481

Welcome to CodeRanch!

A Java class can only have one direct superclass. In your example, Temp extends Dummy. Dummy extends Object. This means Temp indirectly extends Object. But the "extends" keyword only means the direct superclass.

Note that in Java, it is convention to begin class names with uppercase. This will make your code easier for others to read because it will be what they are expecting.

[OCA 8 book] [Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 2
Scott Winterbourne
Ranch Hand

Joined: Jan 30, 2014
Posts: 115

Welcome to the Ranch.

All classes in Java extend java.lang.Object by default.

As for why it shows a class extends Object when it doesn't extend anything else I'm not sure since every class extends Object by default.

When you extend your own class, that class still extends Object also. But it cannot extend more than one class other than Object.

[Edit] Jeanne beat me to the punch
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 43978
raghavan kanagiri wrote: . . . a default constructor is created automatically..... . . .
A default constructor is compiled iff you did not write any constructors yourself.

And welcome again.
subject: Does java extend 2 classes at one time
It's not a secret anymore!