Junior — Middle
Can you explain what is meant by classes in object-oriented programming?
sobes.tech AI
Answer from AI
In object-oriented programming (OOP), a class is a template or blueprint for creating objects. A class defines properties (fields) and behaviors (methods) that the objects of this class will have.
For example, a Car class might have properties: color, brand, speed, and methods: drive, brake, honk. Creating an object of this class results in a specific car with defined characteristics.
Classes allow:
- Encapsulating data and logic together.
- Creating multiple objects with the same structure.
- Using inheritance and polymorphism to extend functionality.
Example in Java:
public class Car {
private String color;
private String model;
public Car(String color, String model) {
this.color = color;
this.model = model;
}
public void drive() {
System.out.println("The car is driving");
}
}
Here, Car is a class, and objects of this class will be specific cars.