Dark Horse programmer--java The foundation---object-oriented polymorphism

Source: Internet
Author: User
Tags define abstract

------<a href= "http://www.itheima.com" target= "blank" >java training, Android training, iOS training,.                                                                                           NET training </a>, look forward to communicating with you! Multi-state of object-oriented

One: The concept of polymorphism

The meaning of polymorphism in practice is that different objects have the same general contour or shape, but the process of concrete execution is quite distinct. Example: Drivers are aware of the "brakes when they encounter a red light" when driving, which is specific to what the driver is driving.

Model of the car is irrelevant, all cars have the same general profile or form of brakes. In Java Development, inheritance-based polymorphism means that the caller of an object function uses a reference to a superclass to make a method call. This can greatly increase the flexibility

, because a superclass reference can invoke the sub-classes of each department, just as a car driver can drive a variety of different cars.

The following example illustrates the problem:

1 //Code Implementation2 //define abstract class car3 Abstract classCar {4     //defining abstract methods Brake5      Public Abstract voidBrake ();6 }7 8 //Define non-abstract class truck inherit from car9 classTruckextendsCar {Ten     //defining the Brake method One      Public voidBrake () { ASystem.out.println ("Braking on the truck!! "); -     } - } the //Define non-abstract class mini inherits from Car class - classMiniextendscar{ -     //implementing the Brake method -      Public voidBrake () { +System.out.println ("is braking on the van!! "); -     } + } A  Public classDemo { at      Public Static voidMain (string[] args) { -         //declares that the car type references C and points it to the object of the truck class -Car C =NewTruck (); -System.out.print ("Call method is:"); -C.brake ();//calling the brake method with reference C -c =NewMini ();//reference C to the object of the Mini class inSystem.out.print ("Call method is:"); -C.brake ();//calling the brake method with reference C to     } +}

The result of the operation is:

The method to invoke is: braking on the truck!!
The method to invoke is: the brakes on the van!!

Description

1) The above code defines three classes of Car,truck and Mini. Where car represents the car, for abstract classes, which have an abstract brake (brake) method,

The signature of the method represents the general pattern of all car brakes.

2) Truck and mini respectively, the car and the mini-van, respectively, the implementation of their parent car in the abstract braking method, to provide different specific sub-class brake implementation.

3) in the main method, the driver stands in the angle of the car (define car type reference c), you can go to the truck (reference C pointing to the truck object)

It is also possible to drive a van (refer to the C pointer to the Mini object).

Second: the substantive meaning and function of the inheritance-based polymorphism in Java:

1) In fact, abstract methods in abstract classes only play a role in the contract. example: Inherit from the automobile non-abstract class, must implement the concrete braking method, otherwise the compilation does not pass.

The number of abstract methods in the parent class is the minimum that the specific subclass needs to implement the number of methods, not less, but can be more than the number of abstract methods in the parent class, such as a method peculiar to some subclasses.

2) because the specific subclass adheres to the contract, for the caller, as long as the reference to the parent class is used , all the different kinds of common energy implemented by all concrete subclasses, the abstract methods in the parent class, the caller

You do not need to know the implementation details in the subclass method.

3) However, if you stand in the perspective of a specific subclass (using a reference to a specific subclass type), then there is less flexibility because a subclass reference of a particular type can only point to objects of that class.

It is inconvenient to use other types of objects.

-----------Android Training, Java training, Java Learning Technology blog, look forward to communicating with you! ------------

Dark Horse Programmer--java Base---Object-oriented 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.