Confusion with implicit extension of Object class

During my study I encountered one confusion with implicit extension of Object class. We all know our classes extend the Object class implicitly or explicitly. That is if we dont extend the Object class, it will automatically be extended.

So this class is available for all of our classes even if we dont extend it why do we need to extend it explicitly. Even I have seen that many of the Java API classes extend it.

Is there any difference b/w implicit and explicit extension of Object class? Can any of you guys please clearify it....

Regards.

[569 byte] By [Christia] at [2007-11-27 10:53:32]
# 1

public class ThingA {}

public class ThingA extends Object {}

there is no difference, when it gets complied the bytecode will be exactly the same.

"extends" is more or less for the complier, different compliers can interperet that keyword differently.

Prior to our lastest generation complier, the only compliers available were from Sun, so there would be no difference. However, now that the compiler has gone public, it is now open to change.

watertownjordana at 2007-7-29 11:44:45 > top of Java-index,Java Essentials,New To Java...
# 2

Thanks..

So it is always better to extend Object class explicitly ?

Christia at 2007-7-29 11:44:45 > top of Java-index,Java Essentials,New To Java...
# 3

It makes no difference whatsoever.

ejpa at 2007-7-29 11:44:45 > top of Java-index,Java Essentials,New To Java...
# 4

> "extends" is more or less for the complier, different

> compliers can interperet that keyword differently.

I'd somehow think that the functional output is the same. Implicit extension means just "extension without saying so", and it probably will not only result in the same compiler output for simplicity's sake, but because the JLS/bytecode specs won't leave it much of a choice.

CeciNEstPasUnProgrammeura at 2007-7-29 11:44:45 > top of Java-index,Java Essentials,New To Java...
# 5

> "extends" is more or less for the complier, different compliers can interperet that keyword differently.

Not if they're supposed to be Java compilers. 'extends' has only one meaning in the JLS.

ejpa at 2007-7-29 11:44:45 > top of Java-index,Java Essentials,New To Java...
# 6

> "extends" is more or less for the complier, different

> compliers can interperet that keyword differently.

Please stop pulling random, incorrect information out of your *** and posting it as fact.

jverda at 2007-7-29 11:44:45 > top of Java-index,Java Essentials,New To Java...
# 7

Your mileage may vary, but I much prefer:

class X {

...

}

to

class X extends Object {

...

}

Why write something that is both redundant and obvious?

Hippolytea at 2007-7-29 11:44:45 > top of Java-index,Java Essentials,New To Java...
# 8

> Even I have seen that many of the Java

> API classes extend it.

In the source code? That's slightly suprising.

Or in the docs? Javadoc puts it there regardless of whether you explicitly did or not, I think.

jverda at 2007-7-29 11:44:45 > top of Java-index,Java Essentials,New To Java...
# 9

> In the source code? That's slightly suprising.

Searching for: extends Object

com\sun\org\apache\xml\internal\serializer\EncodingInfo.java(58): public final class EncodingInfo extends Object

com\sun\org\apache\xml\internal\serializer\Encodings.java(46): public final class Encodings extends Object

com\sun\org\apache\xml\internal\utils\CharKey.java(26): public class CharKey extends Object

java\awt\image\LookupTable.java(29): public abstract class LookupTable extends Object{

java\lang\Character.java(102): class Character extends Object implements java.io.Serializable, Comparable<Character> {

java\util\regex\Pattern.java(2960): static class Node extends Object {

javax\management\Query.java(27): public class Query extends Object{

javax\swing\ProgressMonitor.java(64): public class ProgressMonitor extends Object implements Accessible

javax\swing\plaf\basic\BasicTreeUI.java(3103): public class MouseInputHandler extends Object implements

javax\swing\table\TableColumn.java(68): public class TableColumn extends Object implements Serializable {

javax\swing\text\rtf\RTFGenerator.java(33): class RTFGenerator extends Object

javax\swing\tree\DefaultMutableTreeNode.java(70): public class DefaultMutableTreeNode extends Object implements Cloneable,

javax\swing\tree\DefaultTreeSelectionModel.java(47): public class DefaultTreeSelectionModel extends Object implements Cloneable, Serializable, TreeSelectionModel

javax\swing\tree\TreePath.java(39): public class TreePath extends Object implements Serializable {

Hippolytea at 2007-7-29 11:44:45 > top of Java-index,Java Essentials,New To Java...
# 10

> Even I have seen that many of the Java

> API classes extend it.

That'll be because Sun developers are only human, and do stupid things sometimes. Don't look to the source of the JDK for The Right Way To Do Things ™ cos it ain't there

georgemca at 2007-7-29 11:44:45 > top of Java-index,Java Essentials,New To Java...
# 11

> > In the source code? That's slightly suprising.

> [code]

> Searching for: extends Object

Well, I'll be jiggered.

Not that it's that shocking. I know the API is full of cruft. I just didn't think THAT particular cruft would come from any but day-one coders.

jverda at 2007-7-29 11:44:45 > top of Java-index,Java Essentials,New To Java...
# 12

> Not that it's that shocking. I know the API is full

> of cruft. I just didn't think THAT particular cruft

> would come from any but day-one coders.

It's good to see they're only human. Try searching for "Foo" in the source code.

Hippolytea at 2007-7-29 11:44:45 > top of Java-index,Java Essentials,New To Java...
# 13

Netbeans or possibly Sun Workshop used to do this automatically.

ejpa at 2007-7-29 11:44:45 > top of Java-index,Java Essentials,New To Java...
# 14

> Netbeans or possibly Sun Workshop used to do this automatically.

Name your variables foo?! :-)

Hippolytea at 2007-7-29 11:44:45 > top of Java-index,Java Essentials,New To Java...