Classes and Objects
ICT2622 - Object-Oriented Analysis · Object-Oriented Concepts
Classes and Objects
In object-oriented programming, the concepts of classes and objects are fundamental. A class is a blueprint for creating objects. An object is an instance of a class. This means that a class defines the properties and behaviours that the objects created from it will have.
What is a Class?
A class is a user-defined data type that contains variables and methods. Variables are known as attributes or properties, while methods are functions that define the behaviours of the class. For example, consider a class called Car.
class Car {
string colour;
string model;
int year;
void start() {
// Code to start the car
}
}
In this example, the Car class has three attributes: colour, model, and year. It also has a method called start that defines what happens when the car starts.
What is an Object?
An object is an instance of a class. When you create an object, you allocate memory for it and set its attributes. For example, you can create an object of the Car class as follows:
Car myCar = new Car();
myCar.colour =