2

In java, every class extends Object by default, like that, how we can make our class A can be extends by any class in our application by default.

2
  • Did you mean should - be extended instead of can-be?. All non-final classes can be extended. Commented Oct 15, 2014 at 11:22
  • There is no way of implicitly having every class in your project extends a class. Object is the only class that classes extend by default. You could of course create a class and have every class explicitly extend it. Commented Oct 15, 2014 at 11:25

7 Answers 7

4

The short answer - you can't.

The slightly longer answer - you can create you own custom empty class...

public class MyObject {
}

.. and make every class in your project extend it...

public class MyOtherClass extends MyObject {
    public int doStuff();
}

... but it's really unlikely there'll be any (good) use for this.

Sign up to request clarification or add additional context in comments.

Comments

2

No. The class that other classes extend by default is Object. You cannot change that. It is a fundamental part of the Java language.

The JLS 8.1.4 says:

Given a (possibly generic) class declaration C<F1,...,Fn> (n ≥ 0, C ≠ Object), the direct superclass of the class type C<F1,...,Fn> is the type given in the extends clause of the declaration of C if an extends clause is present, or Object otherwise.

There is no "wriggle room".


If you want all of the classes in your applications to be subclasses of something other than Object, then you must have an extends clause on each and every one of them.

Comments

1

You cannot define an own class as a replacement for the Object class.

You can however create your own base class and let all your classes extend from that base class.

Comments

1

Answer: You Cannot !!

  • Object class is the parent of all the classes in java

  • What ever classes you use is a subclass of object class by default

  • You can guarantee there will always be certain methods, like toString(), equals(), hashCode(), etc.

  • It helps in synchronization, garbage collection etc


Why it is like this:

Java is designed this way so Object class helps by providing all the above mentioned and more features which are fundamental for object communication and object identifying so to have these functionality explicitly extending a class not required


More information:

  1. Have a look at this Stackoverflow answer here
  2. Oracle Docs
  3. Grep Code

Comments

0

There's no way to do that.

You just have to explicitly make every class extend A. like so

public class B extends A {

}

Comments

0

The answer is you can't.

Object class is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class.

Comments

0

It's certainly impossible to do it without explicitly using extends. Object is the parent class of every Java Class, so I think you'll need to stick to it. Apart from that, you can always extend a class with the one you like.

Hope it helps.

Clemencio Morales Lucas.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.