Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Aug 2004
    Location
    The Netherlands
    Posts
    211
    Thanks
    0
    Thanked 1 Time in 1 Post

    Question about inheritance

    When I have two classes: Super and Sub, and I have Sub to extend Super, I get a compile error I'm unable to understand:

    Cannot find symbol - constructor Super()
    Super class:
    Code:
    public class Super
    {
        public Super(String param) {}
    }
    Sub class:
    Code:
    public class Sub extends Super
    {
        public Sub(int param) {}
    }
    Why would I need the constructor Super()?

    Thanks in advance.
    Last edited by Jero; 02-15-2008 at 04:57 PM. Reason: grammar

  • #2
    Regular Coder
    Join Date
    Aug 2004
    Location
    The Netherlands
    Posts
    211
    Thanks
    0
    Thanked 1 Time in 1 Post
    Never mind, one of my friends helped my out with this.

    Just for the record: it appeared I had to call a constructor of the Super class to make it work:

    Code:
    public class Sub extends Super
    {
        public Sub(int param)
        {
            super("Blah.");
        }
    }

  • #3
    New Coder
    Join Date
    Aug 2007
    Posts
    20
    Thanks
    0
    Thanked 1 Time in 1 Post
    If you are defining any constructor in the Sub Class, then the super class needs to have the DEFAULT constructor. Since in the above case, your Super class did not have the default constructor, hence the CTE.

    You can alternatively rectify it by defining the default constructor like :

    public class Super
    {
    // default constructor
    public Super(){}

    public Super(String param) {}

    }


  •  

    Posting Permissions

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