Lecture 3 – Classes and Objects in Practice

Classes and Objects

Learn how to define and use classes and objects in Java and C++. Understand instance variables, methods, and access specifiers public, private, and protected to write structured, secure, and reusable code in this hands-on lecture.

Defining Classes and Creating Objects in Java & C++

In Object-Oriented Programming, a class acts as a template and an object is a real-world instance of that class.

A class contains two main things:

  • Attributes (Data members / instance variables)
  • Methods (Functions / behaviors)
Example (C++):
#include <iostream>
using namespace std;

class Student {
public:
    string name;
    int rollNo;

    void introduce() {
        cout << "Hi, I'm " << name << ", Roll No: " << rollNo << endl;
    }
};

int main() {
    Student s1; // Object creation
    s1.name = "Ali";
    s1.rollNo = 101;
    s1.introduce();

    Student s2;
    s2.name = "Sara";
    s2.rollNo = 102;
    s2.introduce();
}

Explanation:

  • class Student defines what every student has (name, rollNo) and does (introduce()).
  • s1 and s2 are two independent objects of the same class.
Example (Java):
class Student {
    String name;
    int rollNo;

    void introduce() {
        System.out.println("Hi, I'm " + name + ", Roll No: " + rollNo);
    }
}

public class Main {
    public static void main(String[] args) {
        Student s1 = new Student();
        s1.name = "Ali";
        s1.rollNo = 101;
        s1.introduce();

        Student s2 = new Student();
        s2.name = "Sara";
        s2.rollNo = 102;
        s2.introduce();
    }
}

Here too, each object (s1, s2) stores its own data and performs its own actions.

Lecture 2 – Core Concepts of Object-Oriented Programming

Instance Variables and Methods

Instance Variables

These are the attributes that belong to each object separately. Each object has its own copy of these variables.

Example:

Student s1, s2;
s1.name = "Ali";
s2.name = "Sara";

Even though both are from the same class, s1 and s2 hold different data.

Instance Methods

These are the behaviors or actions performed by objects. They often use instance variables to perform operations.

Example:

void introduce() {
    cout << "Hi, I'm " << name;
}

Each object runs this method using its own data.

Access Specifiers (public, private, protected)

Access specifiers control who can access what inside a class.

Access SpecifierAccess LevelDescription
publicEveryoneAccessible anywhere
privateClass onlyHidden from outside
protectedClass + Derived classesUsed in inheritance
C++ Example:
#include <iostream>
using namespace std;

class BankAccount {
private:
    double balance; // Private: cannot be accessed directly

public:
    void setBalance(double amount) {
        if (amount >= 0)
            balance = amount;
        else
            cout << "Invalid amount!" << endl;
    }

    double getBalance() {
        return balance;
    }
};

int main() {
    BankAccount acc;
    acc.setBalance(5000);
    cout << "Your balance is: " << acc.getBalance();
}

This ensures data safety no one can directly modify balance.

Java Example:

class BankAccount {
    private double balance;

    void setBalance(double amount) {
        if (amount >= 0)
            balance = amount;
        else
            System.out.println("Invalid amount!");
    }

    double getBalance() {
        return balance;
    }
}

public class Main {
    public static void main(String[] args) {
        BankAccount acc = new BankAccount();
        acc.setBalance(5000);
        System.out.println("Your balance is: " + acc.getBalance());
    }
}

Access specifiers allow you to hide implementation details while exposing only necessary methods.

Why Access Specifiers Matter

They help:

  • Prevent unauthorized data modification
  • Support Encapsulation
  • Improve security and code readability
  • Allow controlled interaction between objects

Example:

  • A User class might expose getName() but hide password.

Summary of Lecture 3

ConceptDescriptionExample
ClassBlueprint or structureclass Student {}
ObjectInstance of a classStudent s1 = new Student();
Instance VariablesEach object’s own datas1.name, s2.name
Instance MethodsObject behaviorsintroduce()
Access SpecifiersVisibility controlpublic, private, protected

Positive Thought for Students

“Don’t just learn to code learn to think in objects. Once you can visualize code as living things that act, react, and interact programming becomes creativity, not complexity.”

Leave a Reply

Your email address will not be published. Required fields are marked *