Home > Back-end >  Java encapsulation and inheritance
Java encapsulation and inheritance

Time:03-03

Packaging:

In object-oriented programming method, the Encapsulation (English: Encapsulation) refers to a kind of abstract function interface part the implementation details of the packaging, the method of hidden,

Packaging can be thought of as a protective barrier, preventing the class code and data are random access code outside the class definition,

To access the class code and data, must through the strict interface control,

The appropriate packaging can make code easier to understand and maintain, and strengthen the security of the code,

Objective

1. Packaging can hide the implementation details

2. Let the user can only be done by written access methods to access these fields, so we only need to increase the logic control logic in these methods, limit is not reasonable access to data,

3. The convenient data check, help to protect the integrity of object information

4. Easy to modify and improve the maintainability of the code

Implement the Java package steps:

1. Modify the properties of visibility to restrict access to attributes (generally restricted to private)

2. The value of each attribute to provide external access to public methods, namely create getter and setter methods (the first letter of the instance variables to write, add the get or set in front and become a getter and setter method name)

Inheritance:

One, the concept of inheritance

Inheritance is a cornerstone of the Java object-oriented programming technology, because it allows you to create hierarchical levels of classes, inheritance is a subclass inherits the characteristics and behavior of the parent class, making a subclass object (instance) have instance fields and methods of the parent class, or subclass from the parent class method, makes parents with children of the same behavior,

Two, class inheritance format



Three, derived types

It's important to note that Java does not support multiple inheritance, but support multiple inheritance

CodePudding user response:

CodePudding user response:

CodePudding user response:

Say well, speaks a lot better than the book on the market, multiple inheritance speak through the
  • Related