Java Programming thought Learning (vi) polymorphism

Source: Internet
Author: User

1. Three major features of the Java language: inheritance, encapsulation, and polymorphism.

Inheritance: A method of reusing a class that can omission a lot of code;

Encapsulation: Creates a new data type by merging features and behaviors. "The data type" is exactly the same as the 8 "basic data Types" provided by Java itself. Java expands data types by encapsulating this way. 】

Polymorphism: Eliminates the coupling relationship between the innovative data types.

2, early binding: Static and final methods are pre-binding (binding and executing at compile time);

3. Late binding: In addition to the static and final methods in Java, it is late binding (as mentioned earlier, the private method belongs to the final method).

4. Polymorphism:

Only late-bound methods have polymorphism "other, such as: Early binding method, Domain (field) and so on do not have polymorphism";

In other words, polymorphism is an object-oriented feature (one of three major features). And the Java language is not a complete or purely object-oriented language, like static these are anti-object things.

5. Constructor initialization

The constructor of the parent class must be called, first initialized for the parent class, gets the object (child object) of the parent class, and then initializes itself.

6. Transformation:

Upward transformation: new methods are lost;

Downward transformation: Requires type checking.

Example:

1  PackageLKL;2 3  Public classCycle {4         Public intwheels () {5            return0;6        }7 }8 9  Public classTricycleextendscycle{Ten           Public intwheels () { One              return3; A          } - } -  the  Public classBiCycleextendscycle{ -       Public intwheels () { -          return2; -      } + } -  +  Public classUnicycleextendscycle{ A         Public intwheels () { at            return1; -        } - } -  -  Public classRidetest { -           Public intWheels (Cycle cy) { in              returncy.wheels (); -          } to           Public Static voidMain (string[] args) { +Ridetest RT =Newridetest (); -              ///behaves differently depending on the different sub-classes passed in, called Polymorphic theSystem.out.println (Rt.wheels (NewUnicycle ())); *System.out.println (Rt.wheels (NewBiCycle ())); $System.out.println (Rt.wheels (NewTricycle ()));

Java Programming thought Learning (vi) polymorphism

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.