-1

If the Object class contains default constructor generated by compiler then it must have a super(); declaration. If this declaration is there than which parent class constructor is it calling? And how?

Because Object is the parent of all classes, I think if I call super(); in the Object class constructor then it should give us compile time error because we know Object doesn't inherit from anything.

class Test
{
    Test()
    {
        super();
    }
    public static void main(String[] args)
    {
        Test t = new Test();
    }
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • `Object` doesn't have to call a superclass constructor. – user2357112 Sep 14 '17 at 00:01
  • "then it must have a super()" Why? Object is special, in that it is *the ultimate* `super` of everything. – Sergey Kalinichenko Sep 14 '17 at 00:01
  • but every default constructor must have super(); code , – Amarnath Kumar Sah Sep 14 '17 at 00:04
  • _I think if I call super(); in the Object class constructor_ How are you changing `Object` class code? – Sotirios Delimanolis Sep 14 '17 at 00:05
  • All class in java must inherit `java.lang.Object` class by default so there is no problem when yu call `supper()` in your class's constructor. – Bui Anh Tuan Sep 14 '17 at 00:07
  • First tell me is Object class contain default constructor or not – Amarnath Kumar Sah Sep 14 '17 at 00:08
  • if it contains than according to the rule compiler will generate `Test() { super(); }` – Amarnath Kumar Sah Sep 14 '17 at 00:09
  • read [this](https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html), guy! `Class Object is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class.` – Bui Anh Tuan Sep 14 '17 at 00:12
  • @AmarnathKumarSah ask yourself this: "If `Object` _did_ have a default constructor, would that default constructor not also have to call `super()`? And wouldn't that invoke the default constructor of some superclass of `Object`? And wouldn't that superclass constructor then have its own superclass whose constructor would have to be invoked with `super()`? And that superclass would have its own superclass---the super-super-class of `Object`--that would have _its_ constructor... You can see where this is going, yes? It's got to stop somewhere, and that somewhere is `Object`. – Kevin Anderson Sep 14 '17 at 00:18
  • I came across a term 'primordial' objects.After searching I got that a primordial class is that class that has no direct super-class For eg Object. Is this correct? – Amarnath Kumar Sah Sep 14 '17 at 00:26
  • If the class being declared is the primordial class Object, then the default constructor has an empty body. Otherwise, the default constructor simply invokes the superclass constructor with no arguments. – Amarnath Kumar Sah Sep 14 '17 at 00:38

1 Answers1

3

No. JLS-8.8.9. Default Constructor says (in part)

If the class being declared is the primordial class Object, then the default constructor has an empty body. Otherwise, the default constructor simply invokes the superclass constructor with no arguments.

Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • 1
    Here's a trick. If you ever think a JLS quote answers the question, copy a part of that quote and look it up on google with `site:stackoverflow.com`. It helps me find 50% of duplicates. (Though, admittedly, that wasn't necessary here.) – Sotirios Delimanolis Sep 14 '17 at 00:11
  • I came across a term 'primordial' objects.After searching I got that a primordial class is that class that has no direct super-class For eg Object. Is this correct? – Amarnath Kumar Sah Sep 14 '17 at 00:25
  • @AmarnathKumarSah Yes. `java.lang.Object` is the primordial class. – Elliott Frisch Sep 14 '17 at 00:26
  • yes, because of you know i am aware of this also : If the class being declared is the primordial class Object, then the default constructor has an empty body. Otherwise, the default constructor simply invokes the superclass constructor with no arguments. Thanks a lot @Elliott – Amarnath Kumar Sah Sep 14 '17 at 00:36
  • @ElliottFrisch can please tell me : how to know is my class having default constructor or not? is there any way to know? – Amarnath Kumar Sah Sep 14 '17 at 00:42
  • @AmarnathKumarSah If you provide a constructor for the class then it doesn't have a default constructor, if you don't provide any constructors at all then it does. – Elliott Frisch Sep 14 '17 at 01:00