Java Tutorial

Java Object Oriented Programming

OOP models the program as objects: each one holds data and the methods that work on it. A class is the blueprint; an object is one instance built from that blueprint.

A class is a blueprint

Object-oriented programming means you describe types — a player, an account, a cart line — instead of managing a pile of unrelated variables. The class lists the fields and methods; each object is one concrete instance created with new.

Example

class Account {
  String owner;
  double balance;

  void deposit(double amount) {
    balance += amount;
  }
}

public class Main {
  public static void main(String[] args) {
    Account a = new Account();
    a.owner = "Ada";
    a.balance = 100;
    a.deposit(40);
    System.out.println(a.owner + " has " + a.balance);
  }
}

Why bundle data and behavior

Loose int and String variables work until they belong together. A class keeps the balance next to deposit, so you cannot raise one player's score by calling a method meant for another. The next chapters deepen this idea one step at a time.

Example — two objects

class Player {
  String name;
  int score;

  void addPoints(int pts) {
    score += pts;
  }
}

public class Main {
  public static void main(String[] args) {
    Player rin = new Player();
    rin.name = "Rin";
    rin.score = 0;
    rin.addPoints(10);

    Player kai = new Player();
    kai.name = "Kai";
    kai.score = 0;
    kai.addPoints(7);

    System.out.println(rin.name + " " + rin.score);
    System.out.println(kai.name + " " + kai.score);
  }
}

What the OOP section covers

The following lessons build on this sketch: defining classes and objects, instance methods, constructors, access modifiers, encapsulation, inheritance, and polymorphism. Read them in order — each adds one idea without rewriting everything that came before.

You already used classes as data holders earlier. OOP is the same idea plus methods that belong to the object, plus rules for who can see fields and how types relate through inheritance.

Try It Yourself

Exercise: Create a Counter class with an int value field and anincrement() method that adds 1. Create one object, call increment twice, and printvalue.

Show solution
class Counter {
  int value;

  void increment() {
    value++;
  }
}

public class Main {
  public static void main(String[] args) {
    Counter c = new Counter();
    c.increment();
    c.increment();
    System.out.println(c.value);   // 2
  }
}

The method runs on the object, so it updates that object's field — the core OOP habit.

Key Takeaways

  • A class is the blueprint; an object is an instance created with new.
  • OOP bundles data and the methods that operate on it.
  • Each object has its own field values even when they share a class.
  • Later chapters add constructors, privacy, inheritance, and polymorphism.

Worked examples

The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.

Open them in the Java editor at /java/try. Change one measurement and check whether the result still has the right unit.

Engineering

Object owns its state

OOP bundles data and behaviour. A Motor knows its rpm and can report whether it is overspeed.

Example

class Motor {
  int rpm;
  boolean overspeed() { return rpm > 3000; }
}

public class Main {
  public static void main(String[] args) {
    Motor m = new Motor();
    m.rpm = 3200;
    System.out.println(m.overspeed());
  }
}

FAQ: Java Object Oriented Programming

Common questions about this page.

What is the StudyGrid Java tutorial?

The StudyGrid Java tutorial is a full beginner-to-advanced track: syntax, types, input, loops, methods, classes, collections, generics, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run java oop examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn java oop in this Java Java lesson (Java Object Oriented Programming).

Is the Java editor the same as Try Python or Try C++?

No. Try Java compiles with javac at /java/try and shows stdout plus compiler messages. Try Python stays at /try. Try C++ stays at /cpp/try. Java lessons never open those editors.

Do I need to install a JDK to learn Java?

No. Open a chapter, click Try it in Java, and compile in the browser. You can also download a .java file and compile locally with javac.

Where should I start the Java tutorial?

Start at Java Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open Java Examples, then generics, map, and lambdas. Use Next at the bottom of each chapter.

Is the Java tutorial free?

Yes. The Java workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.