Home > Java > javaTutorial > body text

Let's talk about Java encapsulation, inheritance, and polymorphic features

WBOY
Release: 2022-09-06 17:34:52
forward
1460 people have browsed it

This article brings you relevant knowledge about java. It mainly talks about Java encapsulation, inheritance, and polymorphic features. Interested students should take a look. I hope everyone has to help.

Let's talk about Java encapsulation, inheritance, and polymorphic features

Recommended study: "java video tutorial"

1. Encapsulation

What is encapsulation, let’s talk about it My own understanding of encapsulation is that encapsulation hides class information (such as class attributes) inside the class and does not allow direct access by external programs. At this time, we need to mention the keyword private, which is a permission modifier that can be used to modify members (variables and methods) to protect members from being used by other classes. If they need to be used by other classes, then you need to Provide corresponding operations: a. Provide the get variable name () method, used to obtain the value of the member variable; b. Provide the set variable name (parameter), used to set the value of the member variable. It is also the same as the get method, both are used public to modify (also remember that the first letter of the variable name must be capitalized).

2. Inheritance

What is inheritance? Let’s talk about your understanding of inheritance. Inheritance is a consistent relationship between classes, similar to the relationship between inclusion and inclusion in a mathematical set. For example, if college students belong to students, then it can be seen as the college student class inherits the student class. Then the college student class is a subclass (derived class) of the student class, and the student class is the parent class (base class) of the college student class. At the same time, it must be worth Note that inheritance in Java is single inheritance (multi-level inheritance) and multiple implementations (interfaces) (understanding that a class cannot have two fathers, but a son can inherit his father, and the father can inherit multiple levels such as his grandfather)

Format: public class subclass name extends parent class name{}

Example: public class zi extends fu{}

3. Polymorphism

What It is polymorphism. Encapsulation and inheritance are almost all prepared for polymorphism. I personally understand polymorphism as a relationship when a Java program uses inheritance during operation, such as method rewriting. Inheritance is the basis for polymorphism.

For example: We can say that college students are college students: college students unst=new college students ();

We can also say that college students are students: students st=new college students ();

Here college students show different forms at different times. I think this is polymorphism

The prerequisites and manifestations of polymorphism implementation include the following points: 1. There is an inheritance relationship 2. There are methods to rewrite 3. There are parent classes that lead to subclass objects

The following code will show you the three major features of encapsulation, inheritance, and polymorphic java

4. Upload the code, and put the effect execution at the end

package Cl1;
//先创造一个Computer的父类
public class Computer {
    //定义属性,利用private关键字,对类的性质进行封装,
    private String name;
    private int price;
    //构造一个无参方法
    public Computer(){
    }
    //构造一个有参方法
    public Computer(String name) {
        this.name = name;
    }
    //构造一个有参方法
    public String getName() {
        return name;
    }
    //通过set加变量名,首字母大写来设置
    public void setName(String name) {
        this.name = name;
    }
    //通过get加变量名,首字母大写来获取
    public Computer(int price) {
        this.price = price;
    }
    //通过set加变量名,首字母大写来设置
    public void setPrice(int price) {
        this.price = price;
    }
    //通过get加变量名,首字母大写来获取
    public int getPrice() {
        return price;
    }
    //定义父类study方法
    public void study(){
        System.out.println("电脑能用来学习,也能敲代码");
    }
}
Copy after login
package Cl1;
//定义一个Lenovo的子类来继承父类
public class Lenovo extends Computer{
    public Lenovo(){
    }
    public Lenovo(String name){
        super(name);
    }
    //重新study方法,并且同时继承调用父类中的study方法
    public void study(){
        super.study();
        System.out.println(getName()+"价格"+getPrice()+"rmb,"+"敲代码,年入百万不是梦");
    }
}
Copy after login
package Cl1;
//创建一个Apple的子类来继承Computer
public class Apple extends Computer{
    //定义make方法,同时Computer中的s对象 调用study方法
    public void make(Computer s){
        s.study();
    }
}
Copy after login
package Cl1;
//测试类别
public class test {
    public static void main(String[] args) {
        //自动转型(向上转型)
        Computer cp=new Lenovo();
        cp.setName("联想小新");
        cp.setPrice(5000);
        //调用Lenovo中的study方法
        cp.study();
        //多态展现,
        Apple apple=new Apple();
        apple.make(cp);
    }
}
Copy after login

Recommended learning: "java video tutorial"

The above is the detailed content of Let's talk about Java encapsulation, inheritance, and polymorphic features. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:jb51.net
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