GCGrowCarrer

Classes and Objects

Locked

Lesson 9 of 60 · OOP · Medium

Finish these first:Methods →Or pass the tests below to unlock it now.

1

The big idea

A class defines fields and methods; new creates instances. this refers to the current object.

Think of it like this: Think of a Class as a cookie cutter (the blueprint) and each Object as an individual cookie stamped from it — each cookie can have its own frosting (state) while sharing the same shape (methods).
Key words (tap to learn):
2

Read the lesson

A class defines fields and methods; new creates instances. this refers to the current object.

class Counter {
    private int n;
    void inc() { n++; }
    int get() { return n; }
}
3

Quick check

Before writing code: What should `new Solution.Counter().get()` return when your implementation of Classes and Objects is working?

4

Your challenge

2 sample tests, 1 hidden

Implement class Counter with inc() and get().

Stuck? Get a hint (free)
More learning tools(compare languages, visual guide, related lessons)

Compare with another language

Similar to python.classes-objects (80% overlap). Explicit `self` in Python vs implicit `this` in Java; Java fields are declared.

Visual walkthrough

A diagram, a step-by-step animation, and short captions for Classes and Objects.

Mastery0%