Junior — Middle
Can you explain the concept of classes 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 objects of this class will have.
A class combines data and functions that operate on that data into a single structure. This allows modeling real-world entities and their interactions.
Example in C#:
public class Car
{
// Fields (properties)
public string Brand { get; set; }
public string Model { get; set; }
public int Year { get; set; }
// Method
public void Drive()
{
Console.WriteLine($"Car {Brand} {Model} is driving.");
}
}
// Usage
Car myCar = new Car { Brand = "Toyota", Model = "Corolla", Year = 2020 };
myCar.Drive();
Thus, a class defines the structure and behavior of objects, and objects are specific instances of the class with their own field values.