Home  >  Article  >  Java  >  What is abstraction and encapsulation in java

What is abstraction and encapsulation in java

(*-*)浩
(*-*)浩Original
2019-05-22 19:45:434240browse

Abstraction is the process of summarizing the common characteristics of the same class and constructing it into a class; it only cares about member variables and member functions, and does not concern the specific implementation details. Encapsulation is to encapsulate data and operation methods in functions/classes, and then install them; that is, to bind data and methods to operate data, and provide defined interfaces for access to the outside world.

What is abstraction and encapsulation in java

#The four basic characteristics of Java are abstraction, encapsulation, polymorphism, and inheritance. Today we will talk about what abstraction and encapsulation are.

Abstraction

Abstraction: The process of summarizing the common characteristics of the same class and constructing it into a class. Including data abstraction and behavioral abstraction. Data abstraction becomes the member variables of the class, and behavioral abstraction becomes the member functions of the class. Abstraction only cares about member variables and member functions, and has nothing to do with specific implementation details.

The following is Person.class, which represents human-related data and various activities. Only some of the attributes and behaviors are listed.

public class Person{
    // 年龄(数据抽象)
    public int age;
    // 名字(数据抽象)
    public String name;

    public Person(){

    }
    public Person(int age,String name){
        this.age = age;
        this.name = name;
    }
    // 吃东西(行为抽象)
    public void eat(){
        System.out.println("I am eating");   
    }
    // 走路(行为抽象)
    public void walk(){
        System.out.println("I am walking");  
    }
}

Create a human object:

Person person = new Person(18,"张三");

The above code is only used to represent simple abstractions, abstracting out the common attributes and common behaviors of people.

Encapsulation

Encapsulation can actually be understood as: "installation" and "sealing", which means encapsulating data and operation methods in functions/classes, and then installing them . That is to say, the data and methods for operating the data are bound together, and a defined interface is provided for access to the outside world. The essence of object-oriented is to depict the real world as a series of completely autonomous, closed objects. It can be understood that no matter how complicated the internal implementation is, users can ignore it completely and only need to know how to use it.

A small example: The following is a student class. The study method is actually a kind of encapsulation. If the outside world needs students to learn, then just call the study() method directly. There is no need to know how I implement learning. this way.

public class Student implements Serializable {
    public String name;
    public String number;
    public int age;
    public Student() {
    }
    public Student(String name, String number, int age) {
        this.name = name;
        this.number = number;
        this.age = age;
    }
    public void study(){
        System.out.println("我是学生"+name+",我今年"+age+"岁。");
        //内部可能有很复杂的操作,但是调用者只需要关注如何调用即可
    }
}

The above is the detailed content of What is abstraction and encapsulation in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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
Previous article:How to write java classNext article:How to write java class