Classes and Objects in Java (More Examples)

(Based on your video content)


๐Ÿš€ Introduction

In the previous lesson, we learned how to create our first OOP-based Java program using classes and objects.

In this article, we will strengthen that understanding by working with more practical examples. We will cover:

  • Creating classes (blueprints)
  • Creating objects using new
  • Assigning values
  • Calling methods

๐ŸŽฏ Core Idea Recap

In Object-Oriented Programming:

๐Ÿ‘‰ Class = Blueprint
๐Ÿ‘‰ Object = Real Entity

Every object has:

  • State (Data) โ†’ Variables
  • Behavior (Action) โ†’ Methods

๐Ÿ“˜ Example 1: Student Class


๐Ÿ—๏ธ Step 1: Define the Class (Blueprint)

A student has:

  • Name
  • Age

And can perform an action:

  • Study

โœ… Java Code:

class Student {
String name;
int age; void study() {
System.out.println(name + " is studying");
}
}

๐Ÿ” Explanation

  • name, age โ†’ Object variables (data)
  • study() โ†’ Method (behavior)

๐Ÿ‘‰ These belong to the Student class


โš™๏ธ Step 2: Create Object

Student s1 = new Student();

๐Ÿ’ก Important

  • Student โ†’ Class name
  • s1 โ†’ Object name
  • new โ†’ Creates object in memory

๐Ÿ‘‰ Think of new as a factory that builds objects from blueprint


โœ๏ธ Step 3: Assign Values

s1.name = "Alice";
s1.age = 22;

๐ŸŽฎ Step 4: Call Method

s1.study();

๐Ÿ–ฅ๏ธ Output

Alice is studying

๐Ÿ”ฅ Key Concept

๐Ÿ‘‰ Each object has its own data

Student s2 = new Student();
s2.name = "Bob";
s2.age = 20;

Now:

  • s1 โ†’ Alice
  • s2 โ†’ Bob

๐Ÿ‘‰ Same class, different objects, different data


๐Ÿš— Example 2: Car Class


๐Ÿ—๏ธ Step 1: Define the Class

A car has:

  • Model
  • Color

And can:

  • Drive

โœ… Java Code:

class Car {
String model;
String color; void drive() {
System.out.println(color + " " + model + " is driving");
}
}

โš™๏ธ Step 2: Create Object

Car c1 = new Car();

โœ๏ธ Step 3: Assign Values

c1.model = "Toyota";
c1.color = "Red";

๐ŸŽฎ Step 4: Call Method

c1.drive();

๐Ÿ–ฅ๏ธ Output

Red Toyota is driving

๐Ÿ” Universal OOP Pattern

No matter the example, the steps are always the same:

โœ… Step-by-Step Flow

  1. Define class (blueprint)
  2. Create object using new
  3. Assign values
  4. Call methods

๐Ÿ‘‰ Same pattern for:

  • Student
  • Car
  • Robot
  • Any real-world model

โš ๏ธ Common Beginner Mistakes


โŒ Mistake 1: Only Declaration

Student s1;
s1.name = "Alice"; // โŒ ERROR

๐Ÿ‘‰ Problem:

  • Object is NOT created
  • Leads to NullPointerException

โœ… Correct Way:

Student s1 = new Student();

โŒ Mistake 2: Missing Brackets

s1.study;  // โŒ WRONG

โœ… Correct:

s1.study();  // โœ”๏ธ

๐Ÿ”ฅ Key Takeaways

  • Class = Blueprint
  • Object = Real instance
  • new = Creates object in memory
  • . = Access variables and methods
  • Each object has its own data

๐Ÿง  Final Insight

๐Ÿ‘‰ OOP is not just coding
๐Ÿ‘‰ It is modeling real-world concepts in code

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

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