Home  >  Article  >  Java  >  Three major characteristics of object-oriented: encapsulation, inheritance, polymorphism

Three major characteristics of object-oriented: encapsulation, inheritance, polymorphism

王林
王林forward
2020-06-09 17:30:524232browse

Three major characteristics of object-oriented: encapsulation, inheritance, polymorphism

The three major characteristics of object-oriented: encapsulation, inheritance, and polymorphism.

Detailed introduction:

1. Encapsulation

Hide the properties and implementation details of the object, and only provide public access to the outside world.

Benefits: Isolate changes, facilitate use, improve reusability and security.

Principle: Hide content that does not need to be provided externally; hide attributes and provide public methods to access them.

(Recommended video tutorial: java video tutorial)

2. Inheritance

Improve code reusability. Inheritance is a prerequisite for polymorphism.

Note:

1. All constructors in the subclass will access the constructor with empty parameters in the parent class by default. By default, there is super() in the first line; if there is no empty parameter constructor Function needs to be specified in the subclass;

2. In the constructor of the subclass, you can use this to specify other constructors of your own.

3. Polymorphism

is a reference variable defined by a parent class or interface that can point to an instance object of a subclass or a specific implementation class.

Benefits: Improves the scalability of the program.

Disadvantages: When the parent class reference points to the subclass object, although the scalability is improved, only the methods in the parent class can be accessed, and the methods in the subclass cannot be accessed, which is the limitation of access.

Prerequisite: Implementation or inheritance relationship; override parent class method.

Recommended tutorial: java entry program

The above is the detailed content of Three major characteristics of object-oriented: encapsulation, inheritance, polymorphism. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete
Previous article:Can Java make games?Next article:Can Java make games?