Encapsulation, inheritance, polymorphic--java

Source: Internet
Author: User

0. Introduction

The three main features of object-oriented are encapsulation, inheritance and polymorphism. This article takes Java as an example to describe the specific embodiment of these three concepts in the Java language.

First of all, in Java, an object is an instance of a Java class. In a Java class, including data members and method members, where data members are also called properties, method members are referred to simply as methods.

1. Encapsulation

1.1 Concept: encapsulation refers to the properties and methods of the object (that is, the Java Class) combined into a separate whole, hiding the implementation details and providing external access to the interface (interface refers to externally accessible data members and method members).

1.2 Advantages/causes :

(1) Security: The class user can only use the exposed interface to the instance of the class, avoids the class user's improper operation to the class object (eg. the user accidentally modifies a private data member of the object, causing the result to be inconsistent with the expected result when a method of the object is called);

(2) Flexibility: In cases where the exposed interface is not changed, the creator of the class can arbitrarily modify the hidden part without worrying about affecting the class user.

1.3 How to implement encapsulation in Java

The essence of encapsulation is access control, which is designed to control the access of class users to members of a class, including data members and method members. In Java, access control is performed using the public,private,protected three keywords. Where the publickeyword means that the element immediately followed is available to anyone; theprivate keyword means that anyone other than the type creator and the internal method of the type cannot access it;protected keyword Indicates that no one except the type creator, the internal method of the type, and the inheriting class (that is, the subclass) can access it.

In the absence of these three keywords, Java's default access rights are package access, meaning that a member of a class can be accessed by other classes in the same package as the class. (The package is the meaning of the file directory or folder.) )

  Note : Public, private, and protected are access restriction words in Java that can be used not only to decorate data members, method members, but also to modify classes, interface, and so on.

2. Inheritance

2.1 Concept: inheritance refers to the formation of a new class on the basis of an existing class by modifying or adding its data members, method members.

As shown in hierarchy 2.1 of inheritance, the arrows indicate that the derived class inherits the base class. An existing class is called a base class or parent class , and a new class is called an export class , also called a subclass .

Figure 2.1 Inheritance Hierarchy UML diagram

2.2 Possible combinations of subclasses and base classes:

(1) Subclasses and base classes are exactly the same; (this form is relatively uncommon, and subclasses with exactly the same base class are meaningless.) )

(2) Subclasses and base classes provide interfaces that are identical, but subclasses overload or override (overriding, also known as overwrite ) Some methods of the base class; (for overloading and overriding read method overloading and overriding--java )

(3) The subclass adds a new interface on top of the interface provided by the base class externally;

(4) The subclass adds new interfaces and overrides some methods of the base class ((2) and (3);

Summary: Subclasses can make themselves different from the base class by means of either a method override or a newly added data member or method member.

2.3 Advantages/causes : Code reuse (You can create a class similar to the base class by reusing the base class code)

2.4 Note: With inheritance, a subclass object can be considered either as a subclass type or as a base class type. However, a base class object can only be a base class type (unless the subclass is exactly the same as the base class).

3. polymorphic

3.1 Causes: Polymorphism and inheritance are inseparable, in this article 2.2 mentioned in the inheritance of the sub-class and the base class, the method includes overriding the base class method and the new addition data members, method members two ways, the new addition of data members and method members of the way to expand the base class naturally. However, when a subclass overrides a base class method, the method has at least two different implementations, which poses a problem: How does the program choose the specific implementation of the method when it runs the method ? Polymorphic, is to solve this problem arises.

3.2 Concept: polymorphic , also known as late binding , refers to the code that is called when a message is sent to the object until run time. In layman's words, polymorphism means that in a case where a subclass overrides a method of a base class (assuming that the method is named a), a subclass object is created in the program and declared that the object type is a base class type, and a method that invokes the object executes the a method of the subclass, not the A method in the base class.

Polymorphic instances: In the following code, three Java files were created in the same directory: Base.java,sub.java and Test.java. which The base class is created in Base.java, and there is only one method in the base class A;sub.java the Sub class is created by inheriting the base class, and the A method of the base class is overridden in the Sub class, and finally, a sub object is created in the main method in Test.java eg , and Eg is declared as a base type. Call the object a method of eg, by outputting the result, you can see that the Java compiler executes at run time the A method in the sub class, rather than the a method in the base class. This is the manifestation of polymorphism.

1 //base.java:base class2  Public classbase{3    Public voidA () {System.out.println ("This is Base Class.");)}    4 }5 6 //sub.java: Sonclass7  Public classSubextendsbase{8    Public  voidA () {System.out.println ("This is Sub Class."));}9 }Ten  One //test.java:Polymorphic Testing A  Public classtest{ -    Public Static voidMain (string[] args) { -Base eg =NewSub (); the eg. A (); -   }     -}

Output results
The IS Sub Class.

4. Summary

Inheritance and polymorphism are guaranteed by the Java compiler, and program developers only need to understand their ideas and follow the specified rules (through extends implementation of inheritance, through method overrides and overloads to implement polymorphism), while encapsulation requires program developers to complete the implementation.

Encapsulation, inheritance, polymorphic--java

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.