-3

According to Oracle's documentation https://docs.oracle.com/javase/tutorial/java/IandI/super.html, its written that If the super class does not have a no-argument constructor, you will get a compile-time error.

But in my case, I have a super class without any constructor. In my baseclass, I am writing super() in its no-arg constructor. Here, I don't have a no-arg constructor in super class, but its not showing any error.

class Person { 

} 
/* subclass Student extending the Person class */
class Student extends Person { 
    Student() { 
        // invoke or call parent class constructor 
        super(); 
        System.out.println("Student class Constructor"); 
    } 
} 
// Driver class 
class Practice {
    public static void main(String[] args) {
        Student s = new Student(); 
    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • If you don't explicitly create a constructor then Java will silently add the default no-arg constructor for you; you would need a `private Person() {}` constructor (or some other constructor) to override that behavior. – Elliott Frisch Jul 21 '19 at 16:47

1 Answers1

6

This assumption is wrong:

Here, I don't have a no-arg constructor in super class, but its not showing any error.

If a class has no explicit constructor then it will have an implied no-argument constructor.

Please check out this related Stack Overflow question for more: Java default constructor

Also check out the Java Language Specification: §8.8.9. Default Constructor:

If a class contains no constructor declarations, then a default constructor is implicitly declared. The form of the default constructor for a top level class, member class, or local class is as follows:

  • The default constructor has the same accessibility as the class (§6.6).
  • The default constructor has no formal parameters, except in a non-private inner member class, where the default constructor implicitly declares one formal parameter representing the immediately enclosing instance of the class (§8.8.1, §15.9.2, §15.9.3).
  • The default constructor has no throws clauses.
  • 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.
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • Then according to this, the Oracle documentation link which I have sent is wrong – user11316008 Jul 21 '19 at 18:23
  • @user11316008: please tell me which is more likely: 1. the Oracle documentation link that you have is wrong or 2. your understanding of what it states is wrong? Seriously consider these options. I can't speak for you, but I consider myself a not-so-bad coder, and whenever I'm in such a position, the problem is with my understanding 99.999% of the time. Perhaps you're a very expert coder and are much better than me, but I'll wager that the odds are the same for you. – Hovercraft Full Of Eels Jul 21 '19 at 19:41
  • @user11316008: to clarify the issue, you need to 1) quote the section of the link's text, word-for-word, 2) state your understanding of what it means, and then 3) point out why the link is wrong. Trust me, you're interpreting it wrong, just as you appear to be misunderstanding default constructors. – Hovercraft Full Of Eels Jul 21 '19 at 19:43
  • Thank you @Hovercraft Full of Eels. See my understanding is, in the above code Student class (sub class) has a no-arg constructor and it has super() written in it, so it will point to the no-arg constructor of super class. And as in the Person class (super class) we don't have any constructor. So, compiler will automatically create a default constructor. This is the reason why I am not getting any error. Is my understanding correct? – user11316008 Jul 22 '19 at 11:06