Skip to content
On this page

Object-oriented introduction

Object-oriented programming(OOP) is a programming method that maps the real world to a computer model through objects.

In the real world, we define the abstract concept of "person", and the specific people are "Jack", "John" and other specific people. Therefore, "person" can be defined as a class, and a specific person is an instance:

real worldcomputer modelJava code
personclass/classclass Person
Jackinstance/JackPerson ming = new Person()
Johninstance/JohnPerson ming = new Person()

Similarly, "book" is also an abstract concept, so it is a class, and "Java Core Technology", "Java Programming Thoughts", and "Java Study Notes" are examples:

real worldcomputer modelJava code
bookclass/classclass Book
Java core technologyexamples/book1Book book1 = new Book()
Java programming ideasexamples/book2Book book2 = new Book()
Java study notesexamples/book3Book book3 = new Book()

Class and Instance

Therefore, as long as you understand the concepts of class and instance, you basically understand what object-oriented programming is.

Class is an object template that defines how to create instances. Therefore, class itself is a data type: Instance is an object instance. Instance is an instance created based on class. Multiple instances can be created. Each instance has the same type, but their respective attributes may be different:

Define class

In Java, creating a class, for example, naming this class Person , is to define a class :

java
class Person {
    public String name;
    public int age;
}

A class can contain multiple fields ( field ), which are used to describe the characteristics of a class. In the above Person class, we defined two fields, one is String type field named name , and the other is an int type field named age . Therefore, through class , a set of data is gathered into an object to achieve data encapsulation.

public is used to modify the field, which indicates that the field can be accessed externally.

Let’s look at the definition of another Book class:

java
class Book {
    public String name;
    public String author;
    public String isbn;
    public double price;
}

Please indicate the various fields of Book class.

Create instance

Defining a class only defines an object template. To create a real object instance based on the object template, you must use the new operator.

The new operator can create an instance. Then, we need to define a reference type variable to point to this instance:

java
Person jack = new Person();

The above code creates an instance of type Person and points to it through the variable jack .

Note that the distinction between Person jack is to define a variable jack of Person type, and new Person() is to create a Person instance.

With a variable pointing to this instance, we can operate the instance through this variable. You can access instance variables using variable.field, for example:

java
Person jack = new Person();
jack.name = "Jack"; // Assign a value to the field name
jack.age = 12; // Assign a value to the field age
System.out.println(jack.name); // Access field name

Person john = new Person();
john.name = "John";
john.age = 15;

The above two variables point to two different instances respectively. Their structure in memory is as follows:

            ┌──────────────────┐
jack ──────▶│Person instance   │
            ├──────────────────┤
            │name = "Jack"     │
            │age = 12          │
            └──────────────────┘
            ┌──────────────────┐
john ──────▶│Person instance   │
            ├──────────────────┤
            │name = "John"     │
            │age = 15          │
            └──────────────────┘

The two instance have name and age fields defined by class , and each has an independent copy of data without interfering with each other.

Notice

A Java source file can contain the definition of multiple classes, but can only define one public class, and the public class name must be consistent with the file name. If you want to define multiple public classes, they must be split into multiple Java source files.

Practise

Please define a City class with the following fields:

  • name: name, String type
  • latitude: latitude, double type
  • longitude: longitude, double type

Instantiate several cities, assign values, and then print.

java
// City
public class Main {
    public static void main(String[] args) {
        City city = new City();
        city.name = "New York";
        city.latitude = 39.903;
        city.longitude = 116.401;
        System.out.println(city.name);
        System.out.println("location: " + city.latitude + ", " + city.longitude);
    }
}

class City {
    ???
}

Summary

In OOP, class and instance are the relationship between "template" and "instance";

Defining class means defining a data type, and the corresponding instance is an instance of this data type;

field defined by class will have their own field in each instance and will not interfere with each other;

Create a new instance through the new operator, and then use a variable to point to it, so that the instance can be referenced through the variable;

The method to access instance fields is variable.field;

Variables pointing to instance are reference variables.

Object-oriented introduction has loaded