Results 1 to 4 of 4
  1. #1
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    211
    Rep Power
    0

    Default Issue with child class constructor

    Hello,

    Netbeans is returning the following error on the JSOP_ReadStateMachine constructor shown in the code below. Why am I getting that error?

    Stephen

    error: constructor JSOP_StateMachine in class JSOP_StateMachine cannot be applied to given types;
    public JSOP_ReadStateMachine() {
    required: SerialComm
    found: no arguments
    reason: actual and formal argument lists differ in length
    1 error

    Java Code:
    public class SerialComm {
    
    }
    
    public class JSOP_StateMachine {
    
        protected SerialComm serialComm;
     
        public TUCP1_StateMachine(SerialComm serialCommObj) {
            serialComm = serialCommObj;
        }
    
    }
    
    public class JSOP_ReadStateMachine extends JSOP_StateMachine {
    
        int i;
        int j;
    
        public JSOP_ReadStateMachine() {
            i = 0;
            j = 0;
        }
    }

  2. #2
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    211
    Rep Power
    0

    Default Re: Issue with child class constructor

    Ok, I think I understand the issue.

    As far as I know, the parent class constructor is called by default without any arguments. Therefore, If the parent class constructor has arguments, then the child class constructor needs to call the parent class using the super(argument) keyword.

    Is this correct?

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

    Default Re: Issue with child class constructor

    Quote Originally Posted by shall View Post
    Is this correct?
    Sort of. All classes have an implicit, default, no argument constructor to start with. And all subclasses automatically call that constructor from other constructors. However, if you add an argument constructor in the parent class, the default constructor goes away so you have to then explicitly create it manually. So you can either do it your way by calling the parent constructor with arguments, or add in a no-argument constructor to the parent class.

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

  4. #4
    shall is offline Senior Member
    Join Date
    Apr 2012
    Posts
    211
    Rep Power
    0

    Default Re: Issue with child class constructor

    That clears it up.
    Thanks,
    Stephen

Similar Threads

  1. substract Parent class object from child class
    By nikosv in forum New To Java
    Replies: 0
    Last Post: 12-08-2010, 12:30 AM
  2. Replies: 16
    Last Post: 07-08-2010, 09:13 PM
  3. Child-Class Calling a Method in a Parent-Class
    By Blah_ in forum New To Java
    Replies: 5
    Last Post: 09-29-2009, 02:48 AM
  4. Replies: 7
    Last Post: 05-06-2009, 12:42 PM
  5. Replies: 0
    Last Post: 12-19-2007, 09:10 AM

Posting Permissions

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