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 trial
Aaron Brewer
7,939 PointsExplaining Objects, Classes, and Constructors
I saw some students having problems understanding objects, classes, and constructors, so I came up with this little introduction of the concepts.
The easiest way I can explain how classes, objects, and constructors fit together, is by applying this to real life. For example, let's think about a car being made.
All cars share the same sort of features, right? Right! Therefore a car needs a blueprint to define shared features and functionality (behavior or state, which Craig noted). This is where, in Java, we would create the Class, Car.
public class Car {
// This is where our blueprint for ALL cars will live.
}
By default all cars have shared features. These features include; tires, windshields, and an engine. We would say. in Java.. "Whenever this car is built we automatically want to have tires, windshields, and an engine." Below we expand upon the code by adding variables and their initial value.
public class Car {
// Initially setting the type of engine for (not-so-standards) cars.
private String engine = "V8";
// Initially setting the amount of windshields of a standard car.
private int windShields = 6;
// Initially setting amount of tires of a standard car.
private int tires = 4;
}
Now that we have built our Car blueprint, we want to be able to call a method (or function that lives inside of a class) and return the value of any of these features in the blueprint by making it accessible to other classes. We do that below.
public class Car {
// Initially setting the type of engine for (not-so-standards) cars.
private String engine = "V8";
// Initially setting the amount of windshields of a standard car.
private int windShields = 6;
// Initially setting amount of tires of a standard car.
private int tires = 4;
// Return the engine or engine type.
public String getEngine() {
return engine;
}
// Return the amount of wind shields.
public int getWindShields() {
return windShields;
}
// Return the amount of tires.
public int getTires() {
return tires;
}
}
Now we are able to access all of these default features of a car from another class, by (1) creating a new instance of the Car class, and by (2) calling the Car classes methods to return our values.
public class Motorcycle {
public static void main(String[] args) {
// Create a new motorcycle OBJECT based on the CAR class.
Car motorcycle = new Car();
// Print out our defaults set in the CAR class to the terminal.
System.out.printf("The amount of tires our motorcycle has is %s", motorcycle.getTires());
System.out.printf("The amount of windshields our motorcycle has is %s", motorcycle.getWindshields());
System.out.printf("The type of engine our motorcycle has is %s", motorcycle.getEngine());
}
}
Whoa! Wait a second, a motorcycle does not have 4 tires, 6 windshields, but may have a V8 engine. This is where our Constructor, in Java comes into play. Let's create the constructor, so whenever we create (or instantiate) a new object for the motorcycle based on the Car class, we can set the right features for our motorcycle.
public class Car {
// Initially setting the type of engine for (not-so-standards) cars.
private String engine = "V8";
// Initially setting the amount of windshields of a standard car.
private int windShields = 6;
// Initially setting amount of tires of a standard car.
private int tires = 4;
// Creating the constructor, this will run. Whenever we create a new object based on the Car class.
public Car(String engineType, int amountOfWindshields, int amountOfTires) {
engine = engineType;
windShields = amountOfWindshields;
tires = amountOfTires;
}
// Return the engine or engine type.
public String getEngine() {
return engine;
}
// Return the amount of wind shields.
public int getWindShields() {
return windShields;
}
// Return the amount of tires.
public int getTires() {
return tires;
}
}
Now that we have a constructor for our Car class, whenever we are instantiating (or constructing) or Car class for the Motorcycle class, we can now define the features we want for our motorcycle. Let's say that our motorcycle has a V6 engine, has 0 windshields, and has 2 tires. The typical setup for a motorcycle.
public class Motorcycle {
public static void main(String[] args) {
// Create a new motorcycle OBJECT based on the CAR class.
Car motorcycle = new Car("V6", 0, 2);
// Print out our defaults set in the CAR class to the terminal.
System.out.printf("The amount of tires our motorcycle has is %s", motorcycle.getTires());
System.out.printf("The amount of windshields our motorcycle has is %s", motorcycle.getWindshields());
System.out.printf("The type of engine our motorcycle has is %s", motorcycle.getEngine());
}
}
And there we go. Think of objects as new copies of a class, that you may use in other classes. Think of constructors of building the very basic parts of your class as the Class is being instantiated in another class. Think of classes as blueprints for any and everything.
I hope that helps some! :) Thanks, Aaron
3 Answers
Jacob Herrington
15,835 PointsThis might be a helpful reference for many students - this forum is in a Q & A format, but maybe there is the potential for some kind of 'sticky' here?
Zachary Kaufman
1,463 PointsWe definitely need to get this more accessible this is very helpful! Thanks Aaron!
Aaron Brewer
7,939 PointsNo problem! :)