Question:

Define classes in OOP.

Show Hint

A class is a blueprint for creating objects, containing attributes and methods that define their behavior.
Updated On: Sep 7, 2025
Hide Solution
collegedunia
Verified By Collegedunia

Solution and Explanation

In Object-Oriented Programming (OOP), a class is a blueprint or template for creating objects. It defines a type in terms of the data it holds (attributes) and the operations (methods) that can be performed on that data. Classes are fundamental for creating objects, which are instances of a class, and they encapsulate both the state (attributes) and behavior (methods) of the objects.

Step 1: Structure of a Class. A class typically includes: - Attributes (also called fields or properties) that represent the state of an object. - Methods (functions) that define the behavior of the objects. - Constructors for initializing objects of the class.

Step 2: Example of a class. Consider the following simple class in Java: \begin{verbatim} class Car { String model; int year; // Constructor Car(String model, int year) { this.model = model; this.year = year; } // Method void displayInfo() { System.out.println("Model: " + model + ", Year: " + year); } } \end{verbatim} This class defines a blueprint for creating Car objects with two attributes: model and year, and one method: displayInfo, which prints the details of the car.

Was this answer helpful?
0
0