Results 1 to 3 of 3
  1. #1
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    6

    Default Why a compiler error that superclass has no constructor, when every class has one?

    All classes have at least one constructor. If a class does not explicitly declare any, the Java compiler automatically provides a no-argument constructor, called the default constructor. This default constructor calls the class parent's no-argument constructor, or the Object constructor if the class has no other parent. If the parent has no constructor (Object does have one), the compiler will reject the program.
    (source)

    But then, Object is a (direct or indirect) superclass of every class in Java.

    Suppose, we have a class A, which does not extend any class explicitly, so it implicitly extends Object. Also suppose, A does not explicitly provide a constructor, so the compiler adds to it a default constructor automatically, which will call the construtor of its superclass, Object (and Object does have a constructor).

    Now suppose we have a class B which extends class A, and it does not provide an explicit constructor, so the compiler automatically supplies it with a default constructor; this default constructor tries to call a constructor from A.

    Now why a Compiler Error in B, when the compiler has provided a (default) constructor to A (which is calling Object's constructor, and Object has one)?

    Here's a test:

    Compiles Successfully! Does that mean the last sentence in the tutorial is incorrect?

    Java Code:
    class A extends B {
        public static void main(String [] args) {
            //A a = new A();
            System.out.println("Yayyy");
        }
    }
    
    class B {
    }
    Here's my cross-post, but accepting that the Oracle's tutorials could be wrong is difficult and I will appreciate confirmation so that I can be sure that I did not misunderstand something. Thank you.
    Last edited by Zarah; 02-24-2016 at 01:08 AM.

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Why a compiler error that superclass has no constructor, when every class has one

    No, because Class B has a default constructor which is

    Java Code:
    B () {
    }

    Here is an example where the parent class has no default constructor.

    Java Code:
    public class Test extends Foo{
    
       int a;
       public Test(int a) {
          this.a = a;
       }
       public static void main(String[] args) {
          Test t1 = new Test(1);
          Test t2 = new Test();
       }
    
    }
    
    class Foo {
       int b;
       Foo(int b) {
          this.b = b;
       }
    }
    Once you add your own argument constructor, the default goes away so you need to re-add it. Since the default was not re-added
    into Foo, the compiler complains. Both t1 and t2 instantiations cause a compiler error because the default constructor is missing
    from class Foo. The errors are the essentially the same but flagged at different locations in class Test.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    6

    Default Re: Why a compiler error that superclass has no constructor, when every class has one

    @Jim829 got it, thank you very much.

Similar Threads

  1. Compiler error on constructor for thread class
    By ireland94 in forum Threads and Synchronization
    Replies: 2
    Last Post: 05-09-2013, 07:57 PM
  2. compiler error Array of a custom class
    By lenois in forum New To Java
    Replies: 10
    Last Post: 03-10-2013, 05:52 PM
  3. Superclass and Subclass Constructor Question
    By achiu17 in forum New To Java
    Replies: 1
    Last Post: 01-30-2012, 06:05 AM
  4. Is there anyway not to call superclass constructor ?
    By fatabass in forum New To Java
    Replies: 4
    Last Post: 01-29-2012, 10:01 PM
  5. y superclass constructor is called automatically
    By _ShivamKapoOr_ in forum New To Java
    Replies: 1
    Last Post: 09-24-2010, 02:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •