- Objects
- Classes
- Inheritance
-
Programming in Java on Your Computer - Classes and Objects in BlueJ
- Trying out Code in BlueJ
- The Code for Creating and Using Objects
- Source Code and Methods
- Accessors and Mutators
- Constructor Code
- Scope
- Inheritance
-
BlueJ Review - Class Code
- Simple Debugging
- Interactive Picture
- Refactoring Code with Inheritance
Classes
Cars in the real world are rarely custom-made. Instead a blueprint is created for a specific car, such as the Toyota Camry, and then thousands of cars can be created based on it. Each Camry consists of the same components, but can have different details "filled-in". For example, one car might be blue and have a sunroof, while another car might be black and have leather seats.
Similarly, when programming, you do not need to custom-code each object on its own. Instead, you can create Classes as a "blueprint" for each object. In a racing game you can create a Class Racecar
and then create different instances of cars within the game. (Each instance of the Class Car is also known as a Car Object.) So player1
and player2
can each have their own instance of Racecar
which might be called racecar1
and racecar2
. While each car will have the same overall structure and components, they can have different values for their variables, for example:
racecar1:
color = "blue";
sunroof = true;
seats = "vinyl";
racecar2:
color = "black";
sunroof = false;
seats = "leather";
Besides different initial values, each car will have its own current state, so racecar1's speed
will be able to change without any connection to racecar2's speed
. The methods (such as accelerate()
) will be defined in the Class Racecar
, but they will be invoked by each car instance independently. For example, racecar1
might accelerate while racecar2
brakes.
Challenge
A class is like a _________ for creating individual Objects.
Please sign in or sign up to submit answers.
Alternatively, you can try out Learneroo before signing up.
Comments
David
Mar 18, 8:09 AMblueprint -||- blackprint
thales
Jul 8, 8:38 AMyou used || very good :-)