Chapter 8: Classes and Objects in Java 🏷️¶
This cheat sheet covers the core OOP implementation in Java for Chapter 8 of the GSEB Std 12 Computer Studies syllabus.
🎯 Key Concepts¶
- Class: A template for creating objects.
- Object: A specific occurrence (instance) of a class.
new: Operator that creates objects and allocates memory at runtime.- Garbage Collection: Automatic memory cleanup in Java (reclaims memory of unused objects).
🛠️ Java Constructors (Initializers)¶
- Definition: A special method called automatically when an object is created.
- Rules:
- Must have the same name as the class.
- Must not have a return type (no
void,int, etc.).
- Types:
- Default Constructor: Provided by Java if you don't write any. Takes no arguments.
- Parameterized Constructor: Accepts arguments to initialize attributes.
⚙️ Visibility (Access) Modifiers¶
| Modifier | Within Class | Within Package | In Subclass (Inheritance) | Global (Anywhere) |
|---|---|---|---|---|
public |
✅ | ✅ | ✅ | ✅ |
protected |
✅ | ✅ | ✅ | ❌ |
| Default (None) | ✅ | ✅ | ❌ | ❌ |
private |
✅ | ❌ | ❌ | ❌ |
🔒 Static Members & The this Keyword¶
1. The static Modifier¶
- Static Variable: Shared by all objects. Changes made by one object affect all others.
- Static Method: Belongs to the class, not a specific object. Can be called using
ClassName.method(). - Example:
Math.pow(),Math.sqrt().
2. The this Keyword¶
- Refers to the current object in a method or constructor.
- Used to distinguish between instance variables and local variables with the same name.
💡 Board Focus: High-Weightage Points 👔¶
- MCQ Alert:
privatevariables can only be accessed by methods of the same class. - MCQ Alert: Constructors are used to initialize the state of an object.
- MCQ Alert: Memory is allocated using the
newoperator. - MCQ Alert: Static variables are initialized only once when the class is loaded.
- MCQ Alert: A class is a logical entity; an object is a physical entity.
Board Exam Secret
GSEB frequently asks about the Default access modifier. Remember: If you don't write any modifier, it is visible to all classes in the same package.