• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Does java extend 2 classes at one time

 
raghavan kanagiri
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Posts: 34669
366
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Scott Winterbourne
Ranch Hand
Posts: 116
2
Eclipse IDE Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic