Home > Java > javaTutorial > Demystifying the in-depth principles of Java encapsulation and inheritance: exploring the core of object-oriented programming

Demystifying the in-depth principles of Java encapsulation and inheritance: exploring the core of object-oriented programming

PHPz
Release: 2024-03-31 10:16:27
forward
949 people have browsed it

揭秘 Java 封装与继承的深入原理:探索面向对象编程的核心

php editor Baicao brings you a core article that explores the in-depth principles of Java encapsulation and inheritance and explores object-oriented programming. In this article, we will introduce the role and principles of encapsulation and inheritance in Java, and explore how they improve the maintainability and reusability of code. Whether you are a beginner or an experienced developer, this article will provide you with clear explanations and practical application examples to help you better understand and use encapsulation and inheritance in Java. Let's dive into the core of Java object-oriented programming!

Encapsulation is a method of hiding the internal implementation details of a class. It protects data integrity by encapsulating data and operations in a unit (class). In Java, encapsulation is achieved through access modifiers such as private, protected, and public.

  • private: Only visible inside the class.
  • protected: Visible within the class and its subclasses and classes in the same package.
  • public: Accessible in any class.

The benefits of encapsulation include:

  • Data hiding: Protect data from being modified by external code to ensure data consistency.
  • Code reuse: Allows code reuse by using the same encapsulation mechanism in different classes.
  • Improve security: Prevent malicious code from accessing and modifying sensitive data.

inherit

Inheritance is the ability that allows one class (subclass) to inherit the properties and methods of another class (parent class). This provides code reuse and helps create a hierarchical class structure. In Java, inheritance uses extends keyword.

The subclass inherits the following aspects of the parent class:

  • Fields: All non-private fields of the parent class.
  • Methods: All non-private methods of the parent class.
  • Constructor: None.

The benefits of inheritance include:

  • Code reuse: Subclasses do not need to reimplement methods and fields that already exist in the parent class.
  • Polymorphism: Superclass and subclass objects can be treated as the same type, allowing object substitution at runtime.
  • Extensibility: Existing classes can be easily extended by creating new subclasses.

Interaction of encapsulation and inheritance

Encapsulation and inheritance interact in OOP. Encapsulation protects the internal implementation of a class, whereas inheritance allows a child class to access the protected and public members of the parent class. This enables subclasses to reuse and extend the functionality of the parent class while maintaining data hiding and code organization.

In Java, access modifiers are combined with inheritance rules to control subclass access to parent class members:

  • Subclasses can access the public and protected fields and methods of the parent class.
  • Subclasses cannot access the private fields and methods of the parent class.

Example

Consider the following Java code snippet, illustrating the principles of encapsulation and inheritance:

// Person 类(父类)
class Person {
private String name;
protected int age;

public Person(String name, int age) {
this.name = name;
this.age = age;
}

public String getName() {
return name;
}

protected void printAge() {
System.out.println("Age: " + age);
}
}

// Student 类(子类)继承 Person 类
class Student extends Person {
private int studentNumber;

public Student(String name, int age, int studentNumber) {
super(name, age);// 调用父类构造函数
this.studentNumber = studentNumber;
}

public int getStudentNumber() {
return studentNumber;
}

@Override
protected void printAge() {
System.out.println("Student"s age: " + age);
}
}

public class Main {
public static void main(String[] args) {
Student student = new Student("John Doe", 20, 12345);

System.out.println("Student name: " + student.getName());// public 方法,可从子类访问
student.printAge();// protected 方法,可从子类访问
// System.out.println(student.age);// 无法访问 private 字段
}
}
Copy after login

In this example, the Person class encapsulates name and age data and controls access to them through access modifiers. The Student class inherits the Person class and has access to its protected age field and printAge() method. At the same time, the Student class defines a private field studentNumber, which is only visible within itself.

The above is the detailed content of Demystifying the in-depth principles of Java encapsulation and inheritance: exploring the core of object-oriented programming. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:lsjlt.com
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