Results 1 to 11 of 11
Like Tree1Likes
  • 1 Post By JosAH

Thread: Inheritance and constructors

  1. #1
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Inheritance and constructors

    I am starting to use inheritance but am having problems when using the constructors.

    I have 2 classes cla1 and cla2.

    cla2 extends cla1

    The constructor for cla1 takes two ints as parameters
    The constructor for cla2 doesn't take any.


    Java Code:
    public cla1(int v1, int v2){
    		variable1 = v1;
    		variable 2 = v2;
    	}
    
    
    public cla2(){
    		variable1 = 3;
    		variable2 = 0;
    	}

    This is because class two has similar variables to cla1 but they always have the values 0 and 3 in the example.




    When I try to use these though, I get a compiler error saying that the wrong parameters are being used for cla2 when I try to instantiate it with
    Java Code:
    new cla2()

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Inheritance and constructors

    Please copy the full text of the error message and paste it here. It has important info about the error.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Re: Inheritance and constructors

    .\cla2.java:11: error: constructor cla1 in class cla1 cannot be applied to given types;
    public cla2(){
    ^
    required: int,int
    found: no arguments
    reason: actual and formal argument lists differ in length

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,902
    Rep Power
    25

    Default Re: Inheritance and constructors

    Please post ALL of the code for the two classes. The bits and pieces are not enough.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Inheritance and constructors

    For any given class, MyClass, all constructors implicitly call the default constructor MyClass(). The default constructor is provided for you by default. However, if you add any other constructor, the default constructor goes away so you have to put it back in. In your case, Cla1 no longer has a default constructor. But the constructor for Cla2 is trying to invoke it and can't because it doesn't exist. So you need to put it in yourself.

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

  6. #6
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: Inheritance and constructors

    think of Constructor-Chaining

    there must be a constructor in subclass that calls a constructor of the super-class

    if super class only has a constructor that takes two args // Super(int, int)
    In subclass atleast one constructor must call Super(int,int)

    Or as it has been stated:
    you can explicitly put in a constructor with no args in the super-class
    Last edited by Kagiso; 12-01-2013 at 10:00 PM. Reason: correcting a sentence

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Inheritance and constructors

    If cla2 extends cla1 and cla2 only uses the values 3 and 0 for the variables, I'd do this:

    Java Code:
    public cla2() { super(3, 0); }
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    kkid is offline Senior Member
    Join Date
    Jul 2012
    Posts
    241
    Rep Power
    3

    Default Re: Inheritance and constructors

    Quote Originally Posted by JosAH View Post
    If cla2 extends cla1 and cla2 only uses the values 3 and 0 for the variables, I'd do this:

    Java Code:
    public cla2() { super(3, 0); }
    kind regards,

    Jos
    That fixed it, thank you.

    I guess by default it was calling super() which has the wrong number of parameters.
    When I tried it myself I also forgot the parameters but by calling it with the two parameters it fixed the issue.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Inheritance and constructors

    Quote Originally Posted by kkid View Post
    This is because class two has similar variables to cla1 but they always have the values 0 and 3 in the example.
    You should try and avoid giving child classes the same attributes (or attribute names) as the parent.
    It just causes confusion.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Inheritance and constructors

    Quote Originally Posted by Tolls View Post
    You should try and avoid giving child classes the same attributes (or attribute names) as the parent.
    It just causes confusion.
    Duh, if it was hard to write, it should be hard to read ;-)

    kind regards,

    Jos
    masijade likes this.
    cenosillicaphobia: the fear for an empty beer glass

  11. #11
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Inheritance and constructors

    Aka, security by obscurity.

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

Similar Threads

  1. Constructors not right?
    By nikki101 in forum New To Java
    Replies: 2
    Last Post: 05-26-2013, 09:21 AM
  2. Replies: 7
    Last Post: 03-25-2013, 06:35 PM
  3. Help with constructors
    By monopolyman900 in forum New To Java
    Replies: 1
    Last Post: 03-04-2013, 01:21 AM
  4. constructors and inheritance
    By dawnMist in forum New To Java
    Replies: 10
    Last Post: 10-26-2012, 12:39 AM
  5. constructors?
    By shroomiin in forum New To Java
    Replies: 4
    Last Post: 10-13-2009, 03:14 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
  •