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.
Define shared code in one class or method and reuse it instead of copying it.
A class and an object
| Concept | Purpose | Example |
|---|---|---|
| Class | A template that describes data and actions | Knyga |
| Object | A specific instance created from a class | manoKnyga |
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.
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");
}
}Only the class is defined here. No object is created and the method rodytiInformacija() is not called.
public class Knygastarts the definition of a new class.pavadinimasandpuslapiuSkaiciusare class fields: data each object will have.rodytiInformacija()is a method: an action the book object can perform.- A class name starts with an uppercase letter.
- 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);
}
}Mažasis princas
96
Knyga knygadeclares a variable that can refer to aKnygaobject.new Knyga()creates a new specific object from the class definition.- The object receives the initial values defined in the class: a title and a page count.
- The dot in the statement
knyga.pavadinimaslets you access a particular object's field. - 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();
}
}Knyga: Mažasis princas
- First, a specific instance is created:
Knygaobject. - Its method is called using a dot:
rodytiPavadinima(). - The method accesses the same object's field
pavadinimas. - Java combines the text Knyga: with the object's title and displays the result.
- 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);
}
}Mažasis princas
Pepė Ilgakojinė
- Two separate objects are created from the same class:
pirmaKnygaandantraKnyga. - The first book's field stores the title Mažasis princas.
- The second book's field stores the title Pepė Ilgakojinė.
- Each object stores its own separate field value.
- 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();
}
}Mažasis princas
- The file
Knyga.javaholds the class definition, its field and its method. - The file
Main.javacontains the program's entry point,main(). Maincreates a newKnygaobject.- The method is then called through the object:
rodytiPavadinima(). - If both classes are in the same package,
Maincan directly use the accessibleKnygaclass.
What comes next?
Classes and objects are the foundation of OOP. Next you can explore constructors, access modifiers, encapsulation, inheritance, polymorphism and abstraction.