First of all, I would like to state that inheritance, encapsulation, polymorphism is not produced for java,c# or some other language, it is the concept of object-oriented ideas produced.
Let me say, I can only use three words to describe (do not know right, please master pointing):
* Inheritance: Enables subclasses to inherit the properties and methods of the parent class, or to use the functionality of the parent class.
* Encapsulation: Hides the specific implementation, leaving it to the user's interface only.
* Polymorphic: Similar types can exhibit different behavior from the base class when using the same base class method.
have been unable to remember these concepts, Baidu down, put in this for reference:
1. Inheritance (inheritance) inheritance is the technique of building a new class using the definition of an existing class, the definition of a new class can either add new data or new functionality, or it can use the functionality of the parent class, but it cannot selectively inherit the parent class. This technique makes it easy to reuse the previous code, greatly shortening the development cycle and reducing development costs.
Inheritance is intended to reuse the parent class code, while preparing for the implementation of polymorphism.
2, Encapsulation (encapsulation) class makes the data and the operation of the data integrated, so that the other people who use the class, regardless of its implementation, and just use its functions, so that so-called information hiding. Encapsulation hides the internal implementation mechanism of the class so that it can change the internal structure of the class without affecting the consumer, while protecting the data.
3, polymorphism (polymorphism)
The Overrides, overloads, and dynamic joins of a method are polymorphic. Java introduces the concept of polymorphism, one of the reasons is that it is in the inheritance of the class and C + +, which allows multiple inheritance, which does give it a very powerful function, but the complex inheritance of the C + + developers have also brought greater trouble, in order to avoid the risk, Java only allow single inheritance, There is a is-a relationship between the derived class and the base class (that is, "cat" is a "animal"). Although this ensures that the inheritance relationship is simple and clear, but there is bound to be a large number of functional limitations, so Java introduced the concept of polymorphism to compensate for this shortcoming, in addition, abstract classes and interfaces are also important means to solve the single inheritance restrictions. At the same time, polymorphism is also the essence of object-oriented programming. Polymorphism is also divided into design-time polymorphism and run-time polymorphism, such as overloading, also known as design-time polymorphism, and for overrides or inheritance methods, the Java runtime system determines which method is called run-time polymorphism based on the type of instance that invokes the method. In summary, the typical feature of object-oriented design is inheritance, encapsulation and polymorphism, which are the key to the popularity of object-oriented.
For polymorphism, it can be summed up as:
The object that uses the parent class type to point to the child class, which can only invoke methods and variables defined in the parent class ;
If a method in the parent class is overridden in a subclass, the method in the subclass is called when the method is called; (dynamic connection, dynamic invocation)
variables cannot be overridden (overwritten), andthe concept of" overriding " is only for methods.
The understanding of polymorphism:
The individual feels that polymorphism is the most abstract concept, which is explained by examples.
1. Public, the protected method is polymorphic, but if a method is static, it is not polymorphic because the static method is associated with a class rather than a single object.
Packagecom.wx.test; Public classBase { Public voidPublicmethod () {System.out.println ("Base Public Method"); } protected voidProtectedmethod () {System.out.println ("Base protected Method"); } Public Static voidPublicstaticmethod () {System.out.println ("Base protected Method"); }} Public classSub1extendsBase { Public voidPublicmethod () {System.out.println ("Sub1 Public Method"); } protected voidProtectedmethod () {System.out.println ("Sub1 protected Method"); } Public Static voidPublicstaticmethod () {System.out.println ("Sub1 protected Method"); }} Public classSub2extendsBase { Public voidPublicmethod () {System.out.println ("Sub2 Public Method"); } protected voidProtectedmethod () {System.out.println ("Sub2 protected Method"); } Public Static voidPublicstaticmethod () {System.out.println ("Sub2 protected Method"); }} Public classTest { Public Static voidMain (string[] args) {Base base1=NewSub1 (); Base Base2=NewSub2 (); Base1.publicmethod (); Base2.publicmethod (); Base1.protectedmethod (); Base2.protectedmethod (); Base1.publicstaticmethod (); Base2.publicstaticmethod (); }}
View Code
The results of the operation are as follows:
Public public protected protected Protectedprotected Method
View Code
As you can see from the above results, the child class object is converted to a parent object, and the actual subclass method is called when the public and protected method calls. The problem is that the compiler has only one parent class object, and it cannot know which method to invoke. The solution to this problem is late binding, that is, the compiler does not know which method to call, and the runtime only binds the object type to call the subclass method. That is, the compiler has never known the object type, the method invocation mechanism can know the type information, and thus find the correct method body.
2. "Overwrite" private method
Packagecom.wx.test; Public classSuper {Private voidfunction () {SYSTEM.OUT.PRINTLN ("Super Private Method"); } Public Static voidMain (string[] args) {Super sup=NewSub (); Sup.function (); }} Public classSubextendssuper{ Public voidfunction () {SYSTEM.OUT.PRINTLN ("Sub Public Method"); }}
View Code
The results of the operation are as follows:
Private Method
View Code
We expect to output sub public method, but because the private method is automatically considered the final method and is not visible to subclasses, function () in sub is a new method. Therefore, private methods cannot be overwritten.
3. The attribute cannot be polymorphic
Packagecom.wx.test; Public classSuper { Public intfield = 0; Public intGetField () {returnfield;}} Public classSubextendssuper{ Public intfield = 1; Public intGetField () {returnfield;}} Public classTest { Public Static voidMain (string[] args) {Super sup=NewSub (); Sub Sub=NewSub (); System.out.println ("Sup.field =" + Sup.field + "," + "sup.getfield () =" +Sup.getfield ()); System.out.println ("Sub.field =" + Sub.field + "," + "sub.getfield () =" +Sub.getfield ()); }}
View Code
The results of the operation are as follows:
Sup.field = 0,sup.getfield () = 1= 1,sub.getfield () = 1
View Code
In practice, however, this situation generally does not occur, the first is that the attribute is defined as private, so it cannot be accessed directly, and the second is that the subclass does not define properties of the same name as the base class, which is easy to confuse.
Java Multi-State learning