Object-oriented three major features: encapsulation, inheritance, polymorphism (two, polymorphic)

Source: Internet
Author: User

Polymorphic

  One, the concept of polymorphism

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

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

  Second, the realization of polymorphism

  1. Overloading of methods

Method Overload:

    • The parameters must be different. Different number of parameters or different types.
    • The return value can be the same or different.

The value of overloading is that it allows access to a range of related methods by using a flexible method name. When a method is called, the specific method to execute depends on the parameters of the calling method. The Java runtime performs only overloaded methods that match the parameters that are called. Although Java does not stipulate that there must be a connection between overloaded methods, it is customary to overload the same meaning for the readability of the program.

  

For example, the OUTPUT statement print parameter can be any basic type in Java, which is actually an overload of the print () method:

public void Print (Boolean b)

public void print (char c)

public void print (int i)

public void print (long L)

public void print (float f)

public void print (double D)

    • Overloading of construction methods

  Subclasses inherit all member variables and member methods in a superclass, but subclasses cannot inherit the constructor of a superclass. There are two solutions: one is to use the default construction method, and the other is to write several direct constructs.

  2, the method of coverage

There is one in the preceding rule: a subclass inherits all of the member methods in the superclass that can be accessed by subclasses, and if the subclass method has the same name as the superclass method, it cannot inherit, at which point the method of the subclass is called overwrite (override) that method in the superclass. In the hot and sour, should pay attention to the following three points:

    • Subclasses cannot overwrite methods declared as final or static in a superclass.
    • Subclasses must override a method declared as abstract in a superclass, or a subclass is also declared abstract
    • When a 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. That is, the public or protected keywords cannot be changed.

Method overriding is closely related to the inheritance of a class. Coverage embodies the ability of subclasses to supplement or change superclass methods. Overrides enable a method to behave differently between subclasses.

In an object-oriented system, an object encapsulates a method. It is not confusing to use the name object to interpret execution by itself, such as duplicate names and redefinition. This is very advantageous for the requirement analysis and the model design, because these work does not need to involve the concrete data structure and the type, but focuses on the logical rationality of the hint system.

  

    •   The usage difference between this and super references.

This is used in two ways: you can refer to the object itself, such as s1=this, or you can refer to the member variables and methods of this class through this, such as This.count and this.tostring ().

Super has only one usage, that is, to refer to member variables and methods in the superclass by super, such as Super.count and Super.print (), rather than using super alone, such as sl=super is illegal . Because subclasses have an inheritance relationship with a superclass, subclasses inherit variables and methods from the superclass, but the subclass object does not have a similar inheritance relationship to the superclass object , so super has no meaning.

Object-oriented three major features: encapsulation, inheritance, polymorphism (two, polymorphic)

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.