OBJECT-ORIENTED PROGRAMMING

Java OOP: classes and objects

Object-oriented programming (OOP) organizes a program around objects that combine data and the actions performed on it. A class defines the shared structure, while an object is a specific instance of that class.

What is OOP?

Procedural code focuses on methods that act on separately stored data. Object-oriented code combines related data and methods into objects.

Object-oriented programming helps you:

  • give a program a clear, consistent structure;
  • model program concepts as separate objects;
  • reuse classes and their behavior;
  • maintain, change, test and debug code more easily;
  • build larger programs from smaller parts with defined responsibilities.
DRY means “Don't Repeat Yourself”.

Define shared code in one class or method and reuse it instead of copying it.

A class and an object

The difference between a class and an object
ConceptPurposeExample
ClassA template that describes data and actionsKnyga
ObjectA specific instance created from a classmanoKnyga

A single Knyga class can create many different books. They all have the fields and methods defined in the class, but their titles and page counts may differ.

A class is like a blank book information form.

It defines the data every book will have. An object is one completed form, for example the book “The Little Prince”.

Creating a class

A class is created using the keyword class:

public class Knyga {
    String pavadinimas = "Mažasis princas";
    int puslapiuSkaicius = 96;

    void rodytiInformacija() {
        System.out.println(pavadinimas + ": " + puslapiuSkaicius + " puslapiai");
    }
}
There is no console output.

Only the class is defined here. No object is created and the method rodytiInformacija() is not called.

  1. public class Knyga starts the definition of a new class.
  2. pavadinimas and puslapiuSkaicius are class fields: data each object will have.
  3. rodytiInformacija() is a method: an action the book object can perform.
  4. A class name starts with an uppercase letter.
  5. A public class's filename must match the class name: Knyga.java.

Creating an object with new

An object is created by specifying the class type and using the keyword new:

public class Knyga {
    String pavadinimas = "Mažasis princas";
    int puslapiuSkaicius = 96;

    public static void main(String[] args) {
        Knyga knyga = new Knyga();

        System.out.println(knyga.pavadinimas);
        System.out.println(knyga.puslapiuSkaicius);
    }
}
Console output

Mažasis princas
96

  1. Knyga knyga declares a variable that can refer to a Knyga object.
  2. new Knyga() creates a new specific object from the class definition.
  3. The object receives the initial values defined in the class: a title and a page count.
  4. The dot in the statement knyga.pavadinimas lets you access a particular object's field.
  5. The console therefore displays the book title and the number 96.

Calling object methods

The dot operator also calls object methods:

public class Knyga {
    String pavadinimas = "Mažasis princas";

    void rodytiPavadinima() {
        System.out.println("Knyga: " + pavadinimas);
    }

    public static void main(String[] args) {
        Knyga knyga = new Knyga();
        knyga.rodytiPavadinima();
    }
}
Console output

Knyga: Mažasis princas

  1. First, a specific instance is created: Knyga object.
  2. Its method is called using a dot: rodytiPavadinima().
  3. The method accesses the same object's field pavadinimas.
  4. Java combines the text Knyga: with the object's title and displays the result.
  5. The method is not static, so it is called through a specific object, knyga.

Multiple objects of one class

One class can create many independent objects:

public class Knyga {
    String pavadinimas;

    public static void main(String[] args) {
        Knyga pirmaKnyga = new Knyga();
        Knyga antraKnyga = new Knyga();

        pirmaKnyga.pavadinimas = "Mažasis princas";
        antraKnyga.pavadinimas = "Pepė Ilgakojinė";

        System.out.println(pirmaKnyga.pavadinimas);
        System.out.println(antraKnyga.pavadinimas);
    }
}
Console output

Mažasis princas
Pepė Ilgakojinė

  1. Two separate objects are created from the same class: pirmaKnyga and antraKnyga.
  2. The first book's field stores the title Mažasis princas.
  3. The second book's field stores the title Pepė Ilgakojinė.
  4. Each object stores its own separate field value.
  5. Changing one book's title would therefore not change the other book's title.

Using classes in separate files

In a larger program, classes are usually kept in separate files:

// Knyga.java
public class Knyga {
    String pavadinimas = "Mažasis princas";

    void rodytiPavadinima() {
        System.out.println(pavadinimas);
    }
}
// Main.java
public class Main {
    public static void main(String[] args) {
        Knyga knyga = new Knyga();
        knyga.rodytiPavadinima();
    }
}
Console output

Mažasis princas

  1. The file Knyga.java holds the class definition, its field and its method.
  2. The file Main.java contains the program's entry point, main().
  3. Main creates a new Knyga object.
  4. The method is then called through the object: rodytiPavadinima().
  5. If both classes are in the same package, Main can directly use the accessible Knyga class.

What comes next?

Classes and objects are the foundation of OOP. Next you can explore constructors, access modifiers, encapsulation, inheritance, polymorphism and abstraction.