This lesson was translated automatically – it may contain errors.
What is an object (or an instance)?
Back to our car blueprint. If the class is the blueprint, then an object (or an instance) is a particular car built from it.
You can build several different cars from one and the same blueprint: one a red racing car, another a blue truck. In the same way, many objects can be created from a single class.
An object is a concrete entity in the computer’s memory, created from the description a class gives. It has concrete values for its attributes and can perform methods.
Creating objects (instances) of the Car class
namespace Lecture1;
class Program
{
static void Main(string[] args)
{
// Creating two objects (instances) of the Car class
var myCar = new Car("red", "Tesla Model S");
var yourCar = new Car("blue", "Toyota Camry");
// Every object has attribute values of its own
Console.WriteLine(myCar.Color); // Prints: red
Console.WriteLine(yourCar.Model); // Prints: Toyota Camry
// And can perform methods
myCar.StartEngine(); // Prints: Engine started!
yourCar.Drive(); // Prints: Start the engine first!
}
}
int main() {
// Creating two objects (instances) of the Car class – right on the stack
Car my_car("red", "Tesla Model S");
Car your_car("blue", "Toyota Camry");
// Every object has attribute values of its own
std::cout << my_car.color() << std::endl; // Prints: red
std::cout << your_car.model() << std::endl; // Prints: Toyota Camry
// And can perform methods
my_car.StartEngine(); // Prints: Engine started!
your_car.Drive(); // Prints: Start the engine first!
return 0;
}
func main() {
// Creating two objects (instances) of the Car type
myCar := NewCar("red", "Tesla Model S")
yourCar := NewCar("blue", "Toyota Camry")
// Every object has field values of its own
fmt.Println(myCar.Color) // Prints: red
fmt.Println(yourCar.Model) // Prints: Toyota Camry
// And can perform methods
myCar.StartEngine() // Prints: Engine started!
yourCar.Drive() // Prints: Start the engine first!
}
# Creating two objects (instances) of the Car class
my_car = Car("red", "Tesla Model S")
your_car = Car("blue", "Toyota Camry")
# Every object has attribute values of its own
print(my_car.color) # Prints: red
print(your_car.model) # Prints: Toyota Camry
# And can perform methods
my_car.start_engine() # Prints: Engine started!
your_car.drive() # Prints: Start the engine first!
// Creating two objects (instances) of the Car class
const myCar = new Car("red", "Tesla Model S");
const yourCar = new Car("blue", "Toyota Camry");
// Every object has attribute values of its own
console.log(myCar.color); // Prints: red
console.log(yourCar.model); // Prints: Toyota Camry
// And can perform methods
myCar.startEngine(); // Prints: Engine started!
yourCar.drive(); // Prints: Start the engine first!
public class Program {
public static void main(String[] args) {
// Creating two objects (instances) of the Car class
Car myCar = new Car("red", "Tesla Model S");
Car yourCar = new Car("blue", "Toyota Camry");
// Every object has attribute values of its own
System.out.println(myCar.getColor()); // Prints: red
System.out.println(yourCar.getModel()); // Prints: Toyota Camry
// And can perform methods
myCar.startEngine(); // Prints: Engine started!
yourCar.drive(); // Prints: Start the engine first!
}
}
fun main() {
// Creating two objects (instances) of the Car class
val myCar = Car("red", "Tesla Model S")
val yourCar = Car("blue", "Toyota Camry")
// Every object has attribute values of its own
println(myCar.color) // Prints: red
println(yourCar.model) // Prints: Toyota Camry
// And can perform methods
myCar.startEngine() // Prints: Engine started!
yourCar.drive() // Prints: Start the engine first!
}
The main difference between a class and an object
| Class | Object (Instance) |
|---|---|
| It is a template, a description or a concept. | It is a concrete realisation of the class. |
| It is an abstraction (like “a car in general”). | It is a concrete entity (like “my red Tesla”). |
| It is declared once. | It is created many times from a single class. |
| It takes no memory until objects are created. | It takes memory when it is created. |
A simple analogy: the class CookieCutter is a cookie cutter. The objects cookie1, cookie2 are the cookies themselves, made with that cutter.
In summary: a class is the general idea, an object is its material embodiment. Understanding that difference is the key to object-oriented programming. You write classes to describe the entities of your world, and you create objects of those classes to work with them in your program.