Junior — Middle
Explain what a class means in object-oriented programming.
sobes.tech AI
Answer from AI
A class in object-oriented programming is a template or blueprint for creating objects. It describes the data structure (fields, properties) and behavior (methods) that objects of this class will have. The class encapsulates state and logic, allowing the creation of multiple instances with the same set of characteristics but different field values.
Example in Java:
public class Car {
private String model;
private int year;
public Car(String model, int year) {
this.model = model;
this.year = year;
}
public void drive() {
System.out.println(model + " is driving");
}
}
Here, Car is a class, and objects created from it will be specific cars with a given model and year.