Three object-oriented features-inheritance, encapsulation, polymorphism, and ---- Polymorphism

Source: Internet
Author: User

Three object-oriented features-inheritance, encapsulation, polymorphism, and ---- Polymorphism

We have briefly understood the three main characteristics of object-oriented, and I have also roughly explained some of them. Recently, I have seen a deeper explanation on some of the blogs of some other people, I want to add more information.

Sealing

1. encapsulation definition:

Encapsulation is to enclose processes and data. Data access can only be performed through defined interfaces. Object-oriented computing begins with the basic concept that the real world can be depicted as a series of completely autonomous and encapsulated objects that access other objects through a protected interface. Encapsulation is an Information Hiding Technology. in java, the private keyword is used for encapsulation. What is encapsulation? Encapsulation combines all the components of an object, encapsulates how the definition program references the data of the object, and encapsulates the data of the class by actually using methods, controls the extent to which users modify classes and access data.

2. encapsulation:

① The Data encapsulation feature of objects completely eliminates various problems arising from the separation of data and operations in traditional structural methods, and improves the reusability and maintainability of programs, this reduces the burden on programmers to maintain data and operation content. ② The Data encapsulation feature of objects can also split private data and public data of objects, which protects private data and reduces possible inter-module interference, to reduce Program Complexity and improve controllability. Encapsulated objects generally include attributes and Methods. attributes can be used to describe the features of the same class of things. methods can describe the operations that a class of things can perform, encapsulation is to classify the commonalities (including attributes and methods) of the same class into a class for convenient use.
Inheritance: acceptance of common attributes and methods by personality, and addition of unique attributes and methods by Personality
 

1. Concept: A class inherits from another class, And the inherited class is called a subclass. The inherited class is the parent class.
2. purpose: to reuse code.
3. understanding: the relationship between child classes and parent classes is not the parent-child relationship in daily life. Child classes and parent classes are a special and general relationship, which is the relationship between is-, subclass is a more detailed category of the parent class. For example, class dog extends animal can be understood as dog is a animal. note that when designing inheritance, to make a class inherit, the parent class should be appropriately open to access and follow the Rys replacement principle, that is, to disable extension opening to the modification, that is, the open-closed principle.
4. Result: After inheritance, the subclass automatically owns the attributes and methods of the parent class, but note that the private attributes and constructor of the parent class cannot be inherited.
In addition, sub-classes can write their own unique attributes and methods to achieve function expansion. sub-classes can also rewrite the methods of the parent class, that is, method rewriting.

 

Note: JAVA does not support multi-inheritance. Single inheritance makes the inheritance relationship of JAVA very simple. A class can only have one parent class, which is easy to manage programs. At the same time, a class can implement multiple interfaces, to overcome the disadvantages of single inheritance. Classes in Java only support single inheritance, while interfaces support multiple inheritance. Multi-inherited functions in Java are indirectly implemented through interfaces.

 

5. features inherited by java

 

(1) The inheritance relationship is passed. If Class C inherits class B and class B inherits Class A (Multi-inheritance), Class C has the attributes and methods inherited from Class B, there are also attributes and methods inherited from Class A, and new attributes and methods. Although the inherited attributes and methods are implicit, they are still attributes and methods of class C. Inheritance is the most effective way to construct, establish, and expand new classes on the basis of some common classes.

 

(2) Inheritance simplifies people's understanding and description of things, and clearly reflects the hierarchical relationship between classes.

 

(3) Inheritance provides software reuse functions. If Class B inherits Class A, you only need to describe A small number of features (data member and member methods) different from the base class (Class A) when creating Class B. This method can reduce code and data redundancy and greatly increase the reusability of the program.

 

(4) Inheritance reduces inter-module interfaces and interfaces by enhancing consistency, greatly increasing the maintainability of the program.

 

(5) multiple inheritance mechanisms are provided. Theoretically, a class can be a special class of multiple general classes. It can inherit attributes and methods from multiple general classes. This is multi-inheritance. For the sake of security and reliability, Java only supports single-re-inheritance, and implements Multi-inheritance by using the interface mechanism.

 

Polymorphism-- The concept of polymorphism is developed based on encapsulation and inheritance.
Polymorphism is to implement a unified behavior at the abstract level. At the individual (specific) level, this unified behavior will be caused by individual (specific) and implements its own feature behavior. (For an abstract thing, an individual can find its own behavior to execute it .)
1. concept: the same thing calls the same method, and the parameters are the same, but the behavior is different.
2. Understanding: Child classes appear as parent classes, but they are also implemented in their own way when doing things. Sub-classes appear as parent classes and need to be transformed upwards (upcast). The upward transformation is automatically implemented by JVM and is secure, but downcast is insecure, forced conversion is required. When a subclass appears as a parent class, its own attributes and methods cannot be used.

3. There are two manifestations of polymorphism: overload and overwrite.

Overload occurs in the same category. It has nothing to do with what parent class subclass and inheritance.

Besides the function name, a function parameter (number and type) is used to identify a function ). That is to say, a class can have two or more functions called the same name and their parameters are different.

There is no relationship between them. They are different functions, but they may have similar functions. Therefore, they are named the same and increase readability!

Override occurs in the subclass! That is to say, if inheritance is required, overwriting occurs.

We know that we inherit a class, and we have all the methods of the parent class. If you are uncomfortable with the method and want to change the function, implement the function again in the subclass.

In this way, when this method is called, it is the process of executing the subclass. The function in the parent class is overwritten. (Of course, when overwriting, the function name and parameter must be exactly the same as that in the parent class. Otherwise, your method will not have any effect on the methods in the parent class, because the two are two functions and have no relationship)




Batch: if there are similarities, it is a reference

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.