Intermediate⏱️ 11 min📘 Topic 9 of 22

🏛️ Java Classes and Objects — OOP Fundamentals

Master Java classes and objects — fields, methods, constructors, the this keyword, instance vs static members and the four pillars of OOP. Beginner-friendly with examples.

Java is object-oriented to its core — almost everything is a class. A class is a blueprint; an object is an instance created with new.

📜 Anatomy of a class

public class BankAccount {
  private String owner;       // field
  private double balance;

  public BankAccount(String owner, double initial) { // constructor
    this.owner = owner;
    this.balance = initial;
  }

  public void deposit(double amt) { balance += amt; } // method
  public double getBalance() { return balance; }
}

BankAccount a = new BankAccount("Sam", 100);
a.deposit(50);
System.out.println(a.getBalance()); // 150

📍 The this keyword

this refers to the current object — used to disambiguate fields from parameters and to chain constructors with this(...).

⚙️ Instance vs static

  • Instance members belong to each object (each account has its own balance).
  • Static members belong to the class (shared by all): static int count;, Math.PI.

🏛️ The four pillars of OOP

  1. Encapsulation — hide fields, expose methods
  2. Inheritance — derive classes
  3. Polymorphism — one interface, many forms
  4. Abstraction — expose what, hide how

The next topics cover each in depth. If you want pure OOP interview practice, see our Top 50 OOP interview questions.

💻 Code Examples

Constructor + this

class Point {
  int x, y;
  Point(int x, int y) { this.x = x; this.y = y; }
  double dist() { return Math.sqrt(x*x + y*y); }
}
Point p = new Point(3, 4);
System.out.println(p.dist());
Output:
5.0

Static counter shared across instances

class Widget {
  static int count = 0;
  Widget() { count++; }
}
new Widget(); new Widget(); new Widget();
System.out.println(Widget.count);
Output:
3

⚠️ Common Mistakes

  • Making all fields public — breaks encapsulation; keep state private and expose methods.
  • Forgetting to initialize fields — instance fields default to 0/null/false, which may not be valid.
  • Accessing a static member through an instance — works but misleading; use ClassName.member.
  • Shadowing a field with a parameter and forgetting this — `owner = owner` assigns the parameter to itself.

🎯 Interview Questions

Real questions asked at top product and service-based companies.

Q1.What is the difference between a class and an object?Beginner
A class is a blueprint/definition. An object is a concrete instance of that class created with new, holding its own copy of the instance fields.
Q2.What is the this keyword?Beginner
A reference to the current object. It disambiguates fields from parameters (this.x = x), passes the current instance, and chains to another constructor via this(...).
Q3.What's the difference between instance and static members?Intermediate
Instance members belong to each object (separate per instance). Static members belong to the class itself and are shared by all instances — accessed via ClassName.member.
Q4.What are the four pillars of OOP?Intermediate
Encapsulation (hide internal state behind methods), Inheritance (reuse via class hierarchies), Polymorphism (one interface, many implementations), and Abstraction (expose essential behavior, hide details).
Q5.When does the constructor run, and can a class have multiple constructors?Advanced
The constructor runs once when an object is created with new, before the reference is returned. A class can have multiple overloaded constructors (different parameter lists); they can chain with this(...).

🧠 Quick Summary

  • Class = blueprint; object = instance created with new.
  • Constructors initialize objects; this refers to the current object.
  • Instance members are per-object; static members are per-class.
  • Keep fields private (encapsulation), expose behavior via methods.
  • OOP pillars: encapsulation, inheritance, polymorphism, abstraction.