Home > Java > javaTutorial > Object-oriented programming in Java

Object-oriented programming in Java

王林
Release: 2023-06-08 08:03:56
Original
920 people have browsed it

Java is a programming language based on object-oriented programming ideas. Its birth has greatly promoted the development of software development. Object-oriented programming refers to a programming paradigm in which programs are designed and built based on objects and classes. In Java, objects represent real-life entities, while classes are abstract descriptions of objects. In this article, we will explore object-oriented programming in Java.

  1. Classes and Objects

In Java, a class is an abstract data type that contains properties and methods. Variables are attributes of a class, and methods are behaviors of a class. An object is a concrete instance of a class that contains the values ​​of all attributes in a class. For example, we can create a class called Car that contains some properties and methods. When we create a Car object, the object becomes an instance of the Car class and has the properties and methods defined in the Car class.

In Java, the way to create an object is to use the new operator. For example, the following code creates a Car object named myCar:

Car myCar = new Car();
Copy after login

In the above code, Car() is the constructor of the Car class, which is used to initialize the created object.

  1. Inheritance

Inheritance is an important concept in object-oriented programming. In Java, a class can inherit the properties and methods of another class. The inherited class is called a parent class or super class, and the inherited class is called a subclass or derived class. Subclasses can override methods in the parent class, or add their own methods and properties. For example, we can create a subclass called SportsCar that inherits the properties and methods of the Car class and adds some new properties and methods:

public class SportsCar extends Car {
  private boolean turboCharged;

  public SportsCar(String make, String model, int year, boolean turboCharged) {
    super(make, model, year);
    this.turboCharged = turboCharged;
  }

  public boolean isTurboCharged() {
    return turboCharged;
  }

  public void setTurboCharged(boolean turboCharged) {
    this.turboCharged = turboCharged;
  }

  @Override
  public void drive() {
    System.out.println("The sports car is cruising on the highway.");
  }
}
Copy after login

In the above code, the SportsCar class inherits Car class and added a property named turboCharged and two methods named isTurboCharged and setTurboCharged. It also overrides the drive() method in the Car class.

  1. Polymorphism

Polymorphism is another important concept in object-oriented programming. In Java, polymorphism means that an object can be manipulated in multiple ways. It allows subclasses to implement their own methods and also use the methods of the parent class. For example, we can create an abstract class called Vehicle that has a drive() method. We can also create a subclass named Boat and a subclass named Car, and override the drive() method of the Vehicle class:

public abstract class Vehicle {
  public abstract void drive();
}

public class Car extends Vehicle {
  @Override
  public void drive() {
    System.out.println("The car is driving on the road.");
  }
}

public class Boat extends Vehicle {
  @Override
  public void drive() {
    System.out.println("The boat is sailing on the water.");
  }
}
Copy after login

Now, we can create a Vehicle object named myVehicle , and call its drive() method. Depending on the actual situation, myVehicle can be a Car object or a Boat object, and the implementation of their drive() method is different:

Vehicle myVehicle = new Car();
myVehicle.drive(); // 输出 "The car is driving on the road."

myVehicle = new Boat();
myVehicle.drive(); // 输出 "The boat is sailing on the water."
Copy after login
  1. Encapsulation

Encapsulation is object-oriented programming Another important concept in . It is a data hiding technique that ensures that data can only be accessed through methods in the class and restricts direct access to the data. In Java, we can use access modifiers to restrict access to variables and methods in a class. For example, we can declare the make, model and year variables in the Car class as private variables and add public methods named getMake, getModel and getYear:

public class Car {
  private String make;
  private String model;
  private int year;

  public Car(String make, String model, int year) {
    this.make = make;
    this.model = model;
    this.year = year;
  }

  public String getMake() {
    return make;
  }

  public String getModel() {
    return model;
  }

  public int getYear() {
    return year;
  }
}
Copy after login

In the above code, make, model and The year variable is declared as private and can only be accessed through the getMake, getModel and getYear methods. This way, we ensure the security and integrity of our data.

In short, object-oriented programming in Java is of great significance and can help programmers write code more easily and efficiently. In actual development, we need to make full use of the characteristics of object-oriented programming such as classes, objects, inheritance, polymorphism and encapsulation to improve the reliability, maintainability and scalability of the program.

The above is the detailed content of Object-oriented programming in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template