Junior — Middle
Can you explain the concept of a class in object-oriented programming?
sobes.tech AI
Answer from AI
In object-oriented programming, a class is a template or blueprint for creating objects. It describes the properties (fields) and behaviors (methods) that the objects of this class will have. A class combines data and functions that operate on that data into a single structure.
For example, in C#, a class might look like this:
public class Car
{
public string Brand { get; set; }
public int Year { get; set; }
public void Drive()
{
Console.WriteLine($"The {Brand} car is driving.");
}
}
Here, the Car class describes a car with a brand and year of manufacture, as well as a Drive method that outputs a message. Creating an object of this class gives us a specific car with the defined properties and behaviors.