Encapsulation, inheritance, and polymorphism of the (Java) class

Source: Internet
Author: User
Tags modifiers


Encapsulation, inheritance and polymorphism are the three core features of object-oriented programming.

Encapsulation is an important concept in object-oriented technology, it is a kind of information hiding technology, it embodies the idea of object-oriented programming. Inheritance is an important means of object-oriented programming method, it can organize program structure more effectively, clear the relationship between classes, make full use of existing classes to complete more complex and deeper program development. Polymorphism allows for the processing of existing variables and related classes in a unified style, and polymorphism makes it easy to add functionality to the system.

1, packaging

The implication of encapsulation is that the designer of the class designs the class into a black box, the user can see only the public variables and public methods defined in the class, and the details of the implementation of the method are not visible, nor does it operate on the non-public data of the class. This can prevent external interference and misuse.

The purpose of encapsulation is to separate the designer of the object from the designers of the objects, and the user does not need to know the details of the implementation of the behavior, only to access the object with the interface provided by the designer. At the same time, reduce the dependencies between the parts of the program, reduce the complexity of the program, because it hides the details of its internal information, so that internal information is not easy to be destroyed, security has been guaranteed, but also for the outside access provides a simple and convenient interface.

Ø the implementation of encapsulation

Java defines 4 types of access rights: public, protected (protected), private, and default.

Class member access rights

Same class

Same package

Different packets of neutron class

Other Packages

Public

Protected

Default

Private

2. Inherit

Inheritance is a mechanism for creating new classes from existing classes. With inheritance, we can first create a generic class of common properties, and then create a new class with special properties based on that generic class. The new class inherits the state and behavior of the generic class and increases its own state and behavior as needed.

Define a new class from an existing class, called a new class that inherits an existing class, where an existing class that is inherited is called a superclass (superclass) or a parent class, and a class derived from inheritance is called a subclass (subclass).

Ø Inheritance of member variables

Subclasses inherit the member variables that are accessible to all quilts in the parent class. The principles of inheritance are as follows:

1. The ability to inherit those member variables declared as public and protected.

2. Member variables that can inherit the default modifiers from the same package.

3. You cannot inherit member variables that are declared private.

4. If a subclass declares a member variable with the same name as a member variable of the parent class, the member variable of the parent class cannot be inherited. This

The member variable of a subclass is called a member variable that hides the parent class.

In summary, subclasses can inherit the public, protected, and default cosmetic variables of the parent class, and cannot inherit private variables. Conversely, if the parent class does not allow its subclasses to access some of its members, it must declare that member in private. This fully embodies the information concealment principle of class encapsulation.

Ø member method of inheritance

The rules for inheriting member methods of subclasses are similar to those of inherited member variables: subclasses inherit all of the member methods that can be accessed by quilts in the parent class. The inheritance rules are as follows:

1. The ability to inherit those member methods declared as public and protected.

2. Member methods that can inherit the default modifiers from the same package.

3. You cannot inherit member methods that are declared private.

4. You cannot inherit the constructor method of a parent class.

If the subclass method has the same name as the parent class method, it cannot be inherited. A subclass method is called that overrides the method in the parent class.

In summary, subclasses can inherit the public, protected, and default cosmetic methods of their parent classes, and cannot inherit private methods. Subclasses can also increase their own members in addition to the variables and methods that can be inherited from the parent class. When a parent class member does not fit the subclass, the subclass will redefine it, and if it is redefining a member variable is a variable that hides the parent class, if the member method is the method that overrides the parent class.

3, polymorphism

Polymorphism refers to a name that can have multiple semantics. In object-oriented languages, polymorphism is a method in a class in an inheritance tree that can have multiple methods with the same name but different method bodies and different formal parameters. There are usually two ways to implement polymorphism: overloading and overwriting of methods.

Polymorphism allows for the handling of existing variables and related classes in a unified style. Polymorphism makes it easy to add new functionality to the system. Inheritance and polymorphism are effective technologies to reduce software complexity.

Ø method overload When:

The V parameter must be different, i.e. the number of parameters is different, and the type can be different. L

V return values can be the same or different.

The value of overloading is that it allows access to a series of related methods by using a generic method name. When you call a method, which method is executed based on the parameters of the calling method, the Java running system executes only the overloaded method that matches the parameters that are invoked. Although Java does not specify that there must be a connection between overloaded methods, it is customary to overload the same meaning for the readability of the program.

Ø method of Coverage

In the preceding inheritance rule, there is a subclass that inherits all the member methods that can be accessed by the quilt class in the superclass, and cannot inherit if the subclass method has the same name as the superclass method, where the method of the subclass is called overriding (override) the method in the superclass.

The following three points should be noted when making an overlay:

A V subclass cannot overwrite a method declared as final or static in a superclass.

The V subclass must overwrite the method declared abstract in the superclass, or the subclass is also declared abstract. L

When a V subclass overrides a method of the same name in a superclass, the subclass method declaration must be the same as the declaration of the superclass overridden method.

The override of a method is closely related to the inheritance of a class. Overlay embodies the ability of subclasses to complement or change superclass methods. Overriding enables a method to behave differently among different subclasses.

n the difference between overload and overlay

1. The coverage of the method is the relationship between the subclass and the parent class, and is the vertical relation; The overload of the method is the relationship between the methods in the same class and the horizontal relationship.

2. Coverage can only be caused by one method, or by a pair of methods; The overload of a method is the relationship between multiple methods.

3. The override requires the same parameter list; The overload requires a different parameter list.

4, in the coverage relationship, the call to the method body is based on the object type (object corresponding to the storage space type) to determine the overload relationship, based on the invocation of the actual parameter list and formal parameter list to select the method body.

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.