Using Java to implement object-oriented programming--Chapter III polymorphism

Source: Internet
Author: User

1. Polymorphism : (polymorphism):

is characterized by the ability to express various forms;

(Professional parlance:) The same implementation interface (reference type), with different instances and different operations

Refers to multiple states of a reference (type) under different circumstances. It can also be understood that polymorphism refers to a method that is implemented in a different subclass by pointing to a pointer to the parent class.

Polymorphism is the ability of the same behavior to have many different manifestations or forms.

Polymorphism is the same interface, using different instances to perform different operations, polymorphism is the embodiment of various forms of objects.

The advantages of polymorphism:

1. Eliminate the coupling between types

2. Replaceable

3. Extensibility

4. Interface

5. Flexibility

6. Ease of

7 , you can reduce the amount of code in a class

8 , can improve the extensibility and maintainability of code

Three prerequisites for polymorphic presence:

Inherited

Rewrite

Parent class reference to child class object

2. To achieve polymorphism:

Three elements to achieve polymorphism:

Write a parent class and subclass with an inheritance relationship;

Subclasses override the parent class method;

Use the parent class's reference to point to the object of the child class;

Two ways to implement polymorphism (inheritance and interfaces):

To implement polymorphism using the parent class as a method parameter:

Eg : Use polymorphic optimization owner to feed pet;

To implement polymorphism using the parent class as a method return value:

Example (Error):

EG1 : polymorphic Form 1: Parent pet do formal parameters, sub-class to do the argument;

Dog Class (Subclass):

Penguin Class (Subclass):

Cat Class (Subclass):

    

Master class:

    

Test class:

Output Result:

Eg2 : Polymorphic Form 2: Parent class to do reference type, subclass instantiation;

Test class (see EG1 for other classes)

Run results (note the blue section):

Eg3 : Parent class as method return value, self-class instantiation, type automatic conversion;

Master Class (add code to EG1 's master Class):

Test class (see EG1 for other classes):

Operation Result:

3. Conversion:

Transition up: Subclass to parent class conversion;

Automatic transformation

Pet pet=new Dog ();

Eg : Convert down:

Master class:

public class Master {

public void Play (Pet pet) {

if ( pet instanceof Dog) {//If the dog is passed in

Dog dog = (dog) pet;

Dog.catchingflydisc ();

}else if (pet instanceof Penguin) {//If the penguin is passed in

Penguin PGN = (Penguin) pet;

Pgn.swimming ();

}

}

}

Transition down: Conversion of the parent class to a subclass (coercion type conversion):

    Instanceof operator: instanceof is commonly used in conjunction with forced type conversions

Grammar:

object instanceof class or interface     

EG1: ways to achieve your pet's play:

subcategories and Pet classes:     

Master class:

Test class:

Output Result:

Eg2 :

Public class Test {

Public Static void Main (string[] args) {

Show  (new Cat ()); Call the Show method with a Cat object

Show  (new Dog ()); Call the Show method with a Dog object

Animal a = new Cat (); Upward transformation

A.eat (); The eat of the Cat is called

Cat C = (cat) A; Down transformation

C.work (); The catchmouse of the Cat is called

}

Public Static void Show (Animal a) {

A.eat ();

Type judgment

if  (A instanceof Cat) {//cat do things

Cat C = (cat) A;

C.work ();

} Else if (a instanceof dog) {//dog do things

Dog C = (dog) A;

C.work ();

}

}

}

Abstract class Animal {

Abstract void eat ();

}

class Cat extends Animal {

Public void eat () {

System. out. println ("Eat fish");

}

Public void work () {

System. out. println ("Catch Mouse");

}

}

class Dog extends Animal {

Public void eat () {

System. out. println ("Eat Bones");

}

Public void work () {

System. out. println ("housekeeping");

}

}

4. Additional:

Parent class reference points to the subclass object, and the reference object can only call the subclasses to override the parent class's method and cannot invoke the subclass-specific method. Otherwise, it will be an error;

implements polymorphic conditions: the existence of an inheritance, the method of overriding the parent class by a subclass, the reference variable of the parent class to the child class object;

 

Subclass is converted to a parent class (a reference to a parent class is directed to a subclass object), called an upward transformation;

A class inherits from the parent class , and there is a method overloading the procedure, if the subclass has the method at the time of invocation, use the subclass method, if the subclass does not have the method, call the parent class's method;

inheritance system, if you do not specify the call properties and methods with the Super keyword, First in the subclass, then to the parent class;

inheritance and polymorphism in Java. Instantiate the parent class object with the parent class declaration, calling the method in the parent class.

declare subclasses with subclasses and invoke methods in subclasses. The

instantiates the subclass with the parent class declaration, and invokes the overridden method in the subclass.

Subclass object is assigned to the parent class reference, at which point the method called is overridden by the quilt class.

Using Java to implement object-oriented programming--Chapter III 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.