search
HomeJavajavaTutorialHow to implement object-oriented encapsulation in Java?

1. Understand packaging

A simple sentence is the shell shielding details.

For example, a mobile phone contains batteries, chips, motherboards, cameras, screens, and other electronic components.

For our users, just being able to turn on and off the phone, adjust the volume, and make and receive phone calls is enough.

There is no need to consider the internal wiring and electronic components. It doesn’t matter what model it is.

So mobile phone manufacturers will use shells to encapsulate the electronic devices inside to make it more convenient for users to use. It is this idea

and one of our core features of object-oriented The packaging is similar.

More formally speaking: organically combine data and methods of operating data, hide the properties and implementation details of objects, and only expose the interface to interact with objects

According to my popular Understanding is, for example, communicating with someone. I don’t pay attention to how he makes a sound. I only need to listen to what he says.

2. Control access rights -Access modifier

Since encapsulation in Java only leaves the interface to interact with the object, then how to implement this interface and control and hide the details inside, this is where access modifiers are mentioned

The role of access modifiers: Encapsulation is achieved through control classes and access rights

Classes can combine data and methods of encapsulating data,

And access rights are used to control Can methods or fields be used directly outside the class

##Same class in the same package✓✓ ✓✓##Different classes in the same packageSubclasses in different packagesNon-subclasses in different packages

???? (Public) public: The members, methods or classes it modifies can be accessed anywhere.

????(inherited) protected: see the back,

????(default) default: access in the same package

???? (Private) private: can only be accessed in the current class

public class TestDemo01 {
    int year;
    //默认权限,可以省略default
    private int month;
    //私有权限,只能在同一个类中访问
    public int day;
    //公有权限,那里都能访问
}

Demonstrate below

In the first class TestDemo01

How to implement object-oriented encapsulation in Java?

In the two classes TestDemo02

How to implement object-oriented encapsulation in Java?

we found that month cannot be found. This is because we wrote its access permission as private and made it private. The class cannot be accessed. This is the implementation of encapsulation, month can only be used in TestDemo01.

We implement encapsulation in order to leave only the interface and not show the details.

Let’s demonstrate the encapsulated interface

How to implement object-oriented encapsulation in Java?

If It is said that many members are private. We don’t need to write these two interfaces one by one. We can use the function of idea to write them quickly.

How to implement object-oriented encapsulation in Java?

3. Understand the requirements for encapsulation To know - package

3.1 Understand the concept of package

The existence of package is to better manage classes, collect multiple classes together into a group, group them and similar directories

Package is the embodiment of the encapsulation mechanism of classes, interfaces, etc. It is a good way of organizing classes or interfaces, etc.

Package also has a very important role in that it can be used in the same project Classes with the same name are allowed in , as long as they are in different packages.

How to implement object-oriented encapsulation in Java?

3.2 Import classes in the package

How to implement object-oriented encapsulation in Java?

Let’s see why the error is reported

How to implement object-oriented encapsulation in Java?

How to solve this problem, you need to specify a specific package

package com.toabao.www;
import java.util.*;
import java.sql.*;
public class TestDemo03 {
    public static void main(String[] args) {
        int[] array = {1,2,3,4,5};
        System.out.println(Arrays.toString(array));
        java.util.Date date = new java.util.Date();
        java.sql.Date date1 = new java.sql.Date(12);
    }
}

Use import static to import static methods and fields in the package

import static java.lang.Math.*;
public class TestDemo01 {
public static void main(String[] args) {
    double x = 13;
    double y = 14;
    double result = sqrt(pow(x,2)+pow(y,2));
}
}

3.3 Customize the package

Look at the steps first. If you don’t know how, you can try to create a package yourself

How to implement object-oriented encapsulation in Java?

Note:

1. After the package is created , in the file, there must be a package statement at the top to indicate which package the file is in (idea will automatically add it)

How to implement object-oriented encapsulation in Java?

2. The naming of the package is usually For example, it is the reverse name of the domain name

3. We can take a look at the location where the code is stored. The package we just created

How to implement object-oriented encapsulation in Java?

3.4 Access permission control

The same class in the same package

package Demo01;
public class Test01 {
    int count = 10;
    public void func(){
        System.out.println(count);
    }
}

Access of different classes in the same package

How to implement object-oriented encapsulation in Java?

In different packages The access permissions of the subclasses

the previous count are default, that is, default. Subclasses and non-subclasses in different packages cannot be accessed

How to implement object-oriented encapsulation in Java?

If you must access, you can change the access permission to public,

How to implement object-oriented encapsulation in Java?

How to implement object-oriented encapsulation in Java?

3.5 Common packages in java

1. java.lang: Commonly used basic classes in the system (String, Object), you don’t need to write them after JDK1.1, they will be imported automatically,

2. java.lang.reflflect:java reflection programming package

3. java.net: Network programming development package

4. java.sql: Support package for database development

5. java.util: It is a tool package provided by java

6. java.io:I/O programming development package

Scope

private

Private

default

Default

protected

Inherited

public

Public

The above is the detailed content of How to implement object-oriented encapsulation in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
带你搞懂Java结构化数据处理开源库SPL带你搞懂Java结构化数据处理开源库SPLMay 24, 2022 pm 01:34 PM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

Java集合框架之PriorityQueue优先级队列Java集合框架之PriorityQueue优先级队列Jun 09, 2022 am 11:47 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

完全掌握Java锁(图文解析)完全掌握Java锁(图文解析)Jun 14, 2022 am 11:47 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

一起聊聊Java多线程之线程安全问题一起聊聊Java多线程之线程安全问题Apr 21, 2022 pm 06:17 PM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

Java基础归纳之枚举Java基础归纳之枚举May 26, 2022 am 11:50 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

详细解析Java的this和super关键字详细解析Java的this和super关键字Apr 30, 2022 am 09:00 AM

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

Java数据结构之AVL树详解Java数据结构之AVL树详解Jun 01, 2022 am 11:39 AM

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于平衡二叉树(AVL树)的相关知识,AVL树本质上是带了平衡功能的二叉查找树,下面一起来看一下,希望对大家有帮助。

java中封装是什么java中封装是什么May 16, 2019 pm 06:08 PM

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.