Skip to content

Chapter 8: Classes and Objects in Java 🏷️¢

Prerequisites: Basic Java knowledge

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:
    1. Must have the same name as the class.
    2. 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: private variables 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 new operator.
  • 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.


Std 12 Hub | Next: Chapter 9 Cheat Sheet