-
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.
-
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.
-
Re: How to create a sub-class (How to create the constructor, arguments, parameter, e
Quote:
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.
-
Re: How to create a sub-class (How to create the constructor, arguments, parameter, e
Test, don't mind this.
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;
}
}
-
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.
-
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.
-
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
-
Re: How to create a sub-class (How to create the constructor, arguments, parameter, e
-
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.
-
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:
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
-
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