Introduction to OOP
What Is Object-Oriented Programming?
Object-oriented programming (OOP) is a way of organizing code around objects — self-contained units that bundle together data (fields) and the behavior that operates on that data (methods). Instead of writing a long procedure that operates on loose variables, you model your program as a collection of objects that interact with each other, each responsible for its own state and its own logic.
Java was designed around OOP from the ground up. Practically every piece of a Java program — from a simple data holder to an entire application — is expressed as a class, and every value you work with that isn't a primitive is an object of some class.
Classes vs. Objects
A class is a blueprint: it describes what fields and methods something will have, but it does not itself represent any particular thing. An object (also called an instance) is a concrete thing built from that blueprint, with its own actual values stored in its fields.
One class, many objects
public class Dog {
String name;
int age;
}
public class Main {
public static void main(String[] args) {
Dog dog1 = new Dog(); // an object built from the Dog blueprint
dog1.name = "Rex";
dog1.age = 3;
Dog dog2 = new Dog(); // a completely separate object
dog2.name = "Bella";
dog2.age = 5;
}
}Dog is the single blueprint; dog1 and dog2 are two independent objects, each with its own name and age. The full mechanics of writing classes and creating objects are covered in the next page.
The Four Pillars of OOP
Object-oriented languages, including Java, are generally described in terms of four core principles. Each has its own dedicated page later in this course; here's the short version of what each one means.
Pillar | In One Line |
Encapsulation | Bundling data with the methods that operate on it, and hiding internal details behind a controlled public interface |
Inheritance | Letting one class acquire the fields and methods of another, so common behavior can be reused rather than duplicated |
Polymorphism | Allowing a single method call to behave differently depending on the actual runtime type of the object it's called on |
Abstraction | Exposing only the essential behavior of something while hiding the complexity of how it is implemented |
Is Java "Purely" Object-Oriented?
Java is often called object-oriented, but it isn't purely so in the strictest sense. Java has eight primitive types (int, double, boolean, char, and so on) that are not objects at all — they store raw values directly and don't have fields or methods of their own.
Java bridges this gap with wrapper classes: every primitive type has a corresponding object-based counterpart, such as Integer for int, Double for double, and Boolean for boolean. These wrapper classes let primitive values be treated as objects when needed — for example, when storing numbers in a collection that only accepts objects. Aside from this one carve-out for primitives, essentially everything else in Java — strings, arrays, functions represented as objects, exceptions — is modeled as an object.
What's Next
Classes & Objects — the concrete syntax for defining a class and creating instances of it
Constructors — how objects get initialized when they're created
The this Keyword — referring to the current object from inside its own methods
Inheritance, the super Keyword, Polymorphism, and Method Overriding — building relationships between classes