Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialalex kwok
Courses Plus Student 884 Pointscan't not understand how to create a constructor
I can't pass this question , it seem my code is wrong and I'm not sure what I need to make in this section , could you please tell me what is the solution of this topic ? Thank you.
public class Spaceship {
public String mType;
public String getType() {
return mType;
}
public void setType(String type) {
mType = type;
}
private void Abc() {
setType("SHUTTLE");
}
}
2 Answers
Grigorij Schleifer
10,365 PointsHi alex,
a constructor helps you creating abjects and "delivering" arguments for this objects.
So you have a Spaceshift class. This class (and every other classes) is a blueprint how the spaceshift object should look like and what it should do.
To create a constructor you should make it public an use the same name as the class. Also donΒ΄t use a return type.
Your default Spaceship constructor for TASK 1 should take no arguments and look like this:
public Spaceship() {
mType = "SHUTTLE";
}
For TASK 2 the second contructor should look like this:
public Spaceship(String type) {
mType = type;
// the constructor accepts a type String that describes the type of the spaceshift
}
So when you are creating a new Spaceship object you can add a parameter of Type String about the Spaceship type:
Spaceship ship = new Spaceship("destroyer");
// your Spaceship has a destroyer type
I hope this makes sense.
Grigorij
Derek Markman
16,291 PointsThis is unrelated, but it's also worth noting that when using getter and setter methods, it's a bit redundant to make your class' member variables such as your String mType as public. The whole purpose of creating getter and setter methods, is so you can declare it private.
Otherwise, you would just be able to 'get' and 'set' that variable, without the use of the getter and setter methods, in other areas of your code (i.e. other sub-classes, classes, or methods).
public class Spaceship {
private String mType; //Just change this variable to private. :)
//Constructor that takes no parameters.
//(This is the default constructor that is created by the compiler if you don't declare your own).
public Spaceship() {
super();
}
//Overloaded constructor that takes one parameter of type String
public Spaceship(String type) {
mType = type;
}
public String getType() {
return mType;
}
public void setType(String type) {
mType = type;
}
private void Abc() {
setType("SHUTTLE");
}
}
Grigorij Schleifer
10,365 PointsGood point Derek
by making private variables you can protect them from being overriden or changed.
So you will need the setter and getter method to change/retrieve values of the private variable. This way encapsulates the field members and protects them.
G
Derek Markman
16,291 PointsThanks.