Does 'extends Object' have a purpose or is it redundant?

Viewed 28669

Following a tutorial on the internet regarding Soap development with Java, I found this link, with a rather unusual code for myself.

The code:

public class SoapService extends Object {

    /** Creates new SoapService */
    public SoapService() {
    }

/** This is the SOAP exposes method
*/
    public String sayGreeting(String name)
    {
        return "Hello "+name;
    }
}

What's with the 'extends Object' syntax ? I've never encountered this kind of syntax (only on Generics).

Does this syntax has any purpose or is 'plain dumb' ?

18 Answers

Unless the Object class is not actually the java.lang.Object class (the tutorial does not include the imports, so it's hard to see), the extends Object is redundant.

All objects in Java implicitly extend Object, so I'd say it's redundant.

All classes extend Object implicitly anyway so it's just redundant coding having no impact.

Looks a bit like generated code - it's extra effort for a source code generator to omit the "extends" clause if it is not needed, especially if the generator is template-based.

It just means it inherits directly from the Object class. Here is more about inheritance in Java.

No. It's just explicitly doing something that is implicit.

It's unneeded. Every class in Java extends Object at some level. Leave it out, unless you need to clarify something specific.

Extends clause is optional as stated in Java Language Specification. If it is omitted, the class is derived from java.lang.Object. It is just a matter of coding style to write it or not to write it in this case. Usually it is omitted.

It is silly code. Every class in Java extends an Object class. No need to type this explisitly

I think it's redundant.

In Junit source code:

public class TestFailure extends Object {}

I don't know why this class extends Object.

My vote, plain dumb - but then I only play with Java...

But any class inherits from the Object Class as far as I know...

It is legal but useless boilerplate. Everything extends Object so the language spec allows you to leave it out, and it generally should be left out (some writers of coding standards disagree).

The situation is the same in generics (extends Object is implicit and redundant), it is just that for some reason (I have seen some claim early buggy Generics implementations had issues with the ? wildcard) it has caught on a bit more there.

As a matter of fact, it does not seem to be simply redundant, especially when working in the JWS webservices environment.

When defining a class for an XML type to be transported over SOAP, I use the wsimport tool to fetch client dependencies from the WSDL, which creates package-local copies of these classes. By explicitly extending Object, one can seamlessly cast between the classes from the two distinct packages.

Not doing so leads to a compilation error when trying to use a class method from package A that expects an argument type of the class in in package A, and passing in an object generated from the equivalent class in package B.

Related