Java
Module 7: Object-Oriented Programming (OOP)Beginner24 min read

Classes, Objects, Fields & Methods

Design classes and instantiate objects in Java, define instance fields and methods, and understand parameter passing and return types.

Key Takeaways & Core Concepts
  • (blueprints) and objects (runtime instances on the Heap). A class defines state (fields) and behavior (methods).
  • an object does while hiding how it does it.
  • stored in local variable c on the Stack.
  • is copied—so a method can mutate the object's fields, but reassigning the parameter variable does not affect the caller's reference.

Introduction

Object-Oriented Programming (OOP) organizes complex software into cohesive classes (blueprints) and objects (runtime instances on the Heap). A class defines state (fields) and behavior (methods).

Modeling a Course Class & Instantiating Objects
1class Course {
2 String title;
3 int modules;
4
5 void printSummary() {
6 System.out.println(title + " (" + modules + " modules)");
7 }
8}
9
10public class Main {
11 public static void main(String[] args) {
12 Course javaCourse = new Course();
13 javaCourse.title = "CodeZov Java Mastery";
14 javaCourse.modules = 37;
15 javaCourse.printSummary();
16 }
17}
Click code or "Edit Code" to edit
Interactive Code Block•Tab = 4 spaces•Ctrl + Enter = Run
17 lines
Note: `new Course()` allocates memory on the Heap and returns a reference stored in `javaCourse`.

Four Pillars of OOP

  1. 1
    Encapsulation: Protecting internal state behind validated methods.
  2. 2
    Inheritance: Reusing and specializing behavior from a parent class.
  3. 3
    Polymorphism: Treating different concrete types uniformly through a shared interface or base class.
  4. 4
    Abstraction: Exposing what an object does while hiding how it does it.

Object Creation & Pass-by-Value in Java

When you write Course c = new Course();, the new operator allocates memory on the Heap, initializes fields, runs the constructor, and returns a reference stored in local variable c on the Stack. Java is strictly pass-by-value: for primitives, the value is copied; for object references, the reference address is copied—so a method can mutate the object's fields, but reassigning the parameter variable does not affect the caller's reference.

Practice & AssessmentJava Programming Tutorial

Test Your Understanding

Reinforce core concepts with interactive multiple-choice questions, live coding challenges, and top technical interview questions.

Your Current Score

0 / 0 correct(0 attempted)