Classes and Objects

Building blocks of OOP

Overview

Classes and objects are the building blocks of Object-Oriented Programming. A class is a blueprint that defines the structure and behavior, while an object is an instance of a class.

Key Concepts

Class: A template or blueprint for creating objects

Object: An instance of a class with actual values

Attributes: Data members that store the state

Methods: Functions that define behavior

Constructor: Special method to initialize objects

Code Example

java
1// Class definition
2public class Car {
3    // Attributes (instance variables)
4    private String brand;
5    private String model;
6    private int year;
7    private double price;
8    
9    // Constructor
10    public Car(String brand, String model, int year, double price) {
11        this.brand = brand;
12        this.model = model;
13        this.year = year;
14        this.price = price;
15    }
16    
17    // Methods
18    public void start() {
19        System.out.println(brand + " " + model + " is starting...");
20    }
21    
22    public void displayInfo() {
23        System.out.println(year + " " + brand + " " + model + " - $" + price);
24    }
25    
26    // Getters and Setters
27    public String getBrand() {
28        return brand;
29    }
30    
31    public void setPrice(double price) {
32        if (price > 0) {
33            this.price = price;
34        }
35    }
36}
37
38// Creating objects
39public class Main {
40    public static void main(String[] args) {
41        // Object creation
42        Car car1 = new Car("Toyota", "Camry", 2024, 28000);
43        Car car2 = new Car("Honda", "Accord", 2024, 30000);
44        
45        // Using objects
46        car1.start();
47        car1.displayInfo();
48        
49        car2.displayInfo();
50    }
51}

This example shows how to define a Car class with attributes and methods, then create and use car objects.

Real-World Example

Think of a class as a cookie cutter and objects as the actual cookies. The cookie cutter (class) defines the shape and properties, but the cookies (objects) are the actual instances you can eat.

In software: A "User" class defines what a user has (name, email, password) and can do (login, logout). Each actual user in your system is an object created from that class.

Best Practices

  • 1.

    Use meaningful class and variable names

  • 2.

    Follow single responsibility principle - one class, one purpose

  • 3.

    Make attributes private and provide public methods (encapsulation)

  • 4.

    Initialize objects properly using constructors

  • 5.

    Override toString() method for better debugging

💡 Interview Tips

  • Explain the difference between class and object clearly

  • Discuss memory allocation (class in method area, objects in heap)

  • Mention that multiple objects can be created from one class

  • Talk about object lifecycle: creation, use, and garbage collection

AI Tutor

Ask about the topic

Sign in Required

Please sign in to use the AI tutor

Sign In
Classes and Objects - OOP Fundamentals | LLD | Revise Algo