Home  >  Article  >  Java  >  What are the three major characteristics of the Java language?

What are the three major characteristics of the Java language?

烟雨青岚
烟雨青岚Original
2020-07-01 15:26:125797browse

The three major characteristics of the Java language are: encapsulation, inheritance, and polymorphism. Encapsulation is to hide the information of a class inside the class, and does not allow external programs to directly access it. Instead, the hidden information is operated and accessed through the methods of the class; inheritance is a relationship between classes, more like subordinates in a collection. Regarding relationships; polymorphism refers to the multiple forms of objects.

What are the three major characteristics of the Java language?

The three major characteristics of Java language: encapsulation, inheritance, polymorphism

1. Encapsulation

1. Encapsulation is to hide the information of a class inside the class, and does not allow direct access by external programs. Instead, the operation and access to the hidden information is realized through the methods of the class.

2. How is encapsulation implemented?

a. Need to modify the access control character of the attribute (change it to private);

b. Create a getter/setter method (for reading and writing the attribute);

c . Add attribute control statements (used to determine the legality of attribute values) in the getter/setter methods;

public class Student{
    private float score;
    public void setScore(float score){
        this.score = score;
    }
    public float getScore(){
        return score;
    }
}

2. Inheritance

Inheritance is a class A relationship with a class, more like the subordination relationship in a collection. For example, dogs are animals. It can be regarded as the dog class inherits the animal class, then the dog class is the subclass (derived class) of the animal class, and the animal class is the parent class (base class) of the dog class. In Java, there is single inheritance, which means that a subclass has only one parent class.

//父类:动物类
public class Animal{
    public int age;
    public String name;
    public void eat(){
        System.out.println("动物具有吃东西的本领!");
    }
}
//子类:狗类
public class Dog extends Animal {
}

3. Polymorphism

Polymorphism refers to the multiple forms of objects. There are two types of polymorphism: reference polymorphism and method polymorphism. Inheritance is the basis for polymorphism.

1. Reference polymorphism

The reference of the parent class can point to the object of this class; the reference of the parent class can point to the object of the subclass.

//父类:动物类
public class Animal{
    public int age;
    public String name;
    public void eat(){
        System.out.println("动物具有吃东西的本领!");
    }
}
//子类:狗类
public class Dog extends Animal {
}
//测试类
public class Test{
    public static void main(String[] args) {
    //父类的引用可以指向本类的对象
    Animal ani1 = new Animal();
    //父类的引用可以指向子类的对象
    Animal ani2 = new Dog();
    }
}

2. Method polymorphism

When creating a parent class object, the method called is the parent class method;

When creating a subclass object, the method called is the subclass Overridden methods or methods inherited from parent classes;

//父类:动物类
public class Animal{
    public int age;
    public String name;
    public void eat(){
        System.out.println("动物具有吃东西的本领!");
    }
}
//子类:狗类
public class Dog extends Animal {
    public void eat(){
        System.out.println("狗是吃肉的。");
    }
}
//测试类
public class Test{
    public static void main(String[] args) {
    //父类的引用可以指向本类的对象
    Animal ani1 = new Animal();
    //父类的引用可以指向子类的对象
    Animal ani2 = new Dog();
    //输出动物具有吃的本领
    ani1.eat();
    //输出狗是吃肉的
    ani2.eat();
    }
}

Recommended tutorial: "java tutorial"

The above is the detailed content of What are the three major characteristics of the Java language?. 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