Results 1 to 11 of 11
  1. #1
    Ray
    Ray is offline Member
    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0

    Default How to create a sub-class (How to create the constructor, arguments, parameter, etc.)

    I'm not sure if my title made sense.
    Below you will find my superclass and subclass. My superclass is fine.
    When I compile my subclass, I get the following error:

    constructor Bicycle in class Bicycle cannot be applied to given types
    required:int,int found: no arguments reason: actual and formal arguments differ in length.

    Also, if someone could teach me the neat way to post my code so that it shows all the color coding and is almost like a screen shot of bluej.

    Here is my superclass:

    public class Bicycle
    {

    public int speed;
    public int gear;

    public Bicycle(int startSpeed, int startGear)
    {
    speed = startSpeed;
    gear = startGear;
    }

    public void changeSpeed(int newSpeed)
    {
    speed = newSpeed;
    }

    public void changeGear(int newGear)
    {
    gear = newGear;
    }
    }

    Here is my subclass:

    public class MountainBike extends Bicycle
    {

    private int chairHeight;

    public MountainBike(int newChairHeight)
    {
    chairHeight = newChairHeight;
    }
    }

    Sorry, I'm new and I understand the error, but I do not know the syntax and how java works.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default Re: How to create a sub-class (How to create the constructor, arguments, parameter, e

    Please edit the code and wrap it in code tags. See: BB Code List - Java Programming Forum

    What statement is the error on? The only constructor for the Bicycle class takes 2 int for args. When you try to call that constructor you must use that constructor. The MountainBike class's constructor needs to call its super with two ints.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    Ray
    Ray is offline Member
    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0

    Default Re: How to create a sub-class (How to create the constructor, arguments, parameter, e

    When you try to call that constructor you must use that constructor. The MountainBike class's constructor needs to call its super with two ints.
    Yeah, I want to make a mountainbike object. I figured since a subclass gets the fields and variables of a superclass, when creating the object mountainbike, it'd automatically have arguments for the bicycle.

    The error is on line:
    public MountainBike(int newChairHeight)
    { // <-Here, the bracket.
    chairHeight = newChairHeight;
    }

    Sorry, I'll learn the BB code in two seconds after this.

    I tried adding this:
    public MountainBike(int newchairHeight, int speed, int gear)
    but to no avail.
    I guess I really dn't know what im asking. The only way I can phrase it is, where do I add the arguments for Bicycle in the constructor for mountainbike. I don't know if that makes sense.

  4. #4
    Ray
    Ray is offline Member
    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0

    Default Re: How to create a sub-class (How to create the constructor, arguments, parameter, e

    Test, don't mind this.
    Java Code:
    public class Bicycle
    {
        
        public int speed;
        public int gear;
        
        public Bicycle(int startSpeed, int startGear)
        {
            speed = startSpeed;
            gear = startGear;
        }
    
        public void changeSpeed(int newSpeed)
        {
            speed = newSpeed;
        }
        
        public void changeGear(int newGear)
        {
            gear = newGear;
        }
    }

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default Re: How to create a sub-class (How to create the constructor, arguments, parameter, e

    You need to have the MountainBike constructor call its super with the two ints that the Bicycle class's constructor requires.
    Or you could define a Bicycle class constructor with no args that would work with the current MountainBike constructor.
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    Ray
    Ray is offline Member
    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0

    Default Re: How to create a sub-class (How to create the constructor, arguments, parameter, e

    Okay, so does this happen inside the brackets of the declaration (I think they are called parameters) of the mountainbike constructor?
    Or do I call the super inside the mountainbike constructor?

    Would you please give me the exact syntax? Or even better just write it out for me. I learn best by example.

    All I want to do is create an instance of the class mountainbike, but I don't know how to do that because I don't know where to add in the arguments for superclass bike.
    Last edited by Ray; 09-03-2012 at 07:30 PM.

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default Re: How to create a sub-class (How to create the constructor, arguments, parameter, e

    Did you try adding a no arg constructor to Bicycle?


    See the tutorial: http://docs.oracle.com/javase/tutori...ndI/super.html
    If you don't understand my response, don't ignore it, ask a question.

  8. #8
    Ray
    Ray is offline Member
    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0

    Default Re: How to create a sub-class (How to create the constructor, arguments, parameter, e

    Nvr mind
    Last edited by Ray; 09-03-2012 at 07:57 PM. Reason: reading the tutorial you linked. The ending is interesting

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default Re: How to create a sub-class (How to create the constructor, arguments, parameter, e

    That's your decision. Some classes can use default values (with a no arg constructor) others must have some constructor args.
    If you don't understand my response, don't ignore it, ask a question.

  10. #10
    Ray
    Ray is offline Member
    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0

    Default Re: How to create a sub-class (How to create the constructor, arguments, parameter, e

    Okay! That tutorial you linked was really helpful. I get it. You've got to have gear, speed, and chairheight in the arguments of the constructor for mountainbike, and then the constructor will in turn send that to create another object in the superclass:
    Java Code:
    public class MountainBike extends Bicycle
    {
    
        private int chairHeight;
    
        public MountainBike(int newChairHeight, int startSpeed, int startGear)
        {
            super(startSpeed, startGear);
            chairHeight = newChairHeight;
        }
    }
    Thanks for the help

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,586
    Rep Power
    23

    Default Re: How to create a sub-class (How to create the constructor, arguments, parameter, e

    Do you have a link to the tutorial's Big Index?
    The Really Big Index
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. How to create a JFrame window and create a circle.
    By tehsumo in forum AWT / Swing
    Replies: 3
    Last Post: 02-27-2012, 04:06 PM
  2. Replies: 2
    Last Post: 01-06-2012, 11:10 PM
  3. Create an object without calling the constructor?
    By sublixt in forum New To Java
    Replies: 7
    Last Post: 10-28-2011, 12:49 PM
  4. How to create main class link to another two class?
    By pearllymary78 in forum New To Java
    Replies: 6
    Last Post: 07-16-2008, 11:02 PM
  5. Replies: 16
    Last Post: 05-08-2008, 09:45 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
  •