Java Fundamentals (iii)-----Polymorphism of three main features of Java object-oriented

Source: Internet
Author: User

Package (encapsulation)class makes the data and the operation of the data bundled together, so that the other people who use the class, regardless of its implementation method, and just use its function, so that the so-called information hiding;Inheritance (inheritance)Inheritance is the technique of building a new class using the definition of an existing class, and the definition of a new class can add new data or new functionality, or it can use the functionality of the parent class, but not selectively inherit the parent class. This technique makes it easy to reuse the previous code, greatly shortening the development cycle and reducing development costs. For example, you can first define a class called Animal,animal with the following properties: eyes, nose, ears and the animal this class derives the dog and cat two classes, adds a housekeeping function to the dog, and adds a catch fish function to cat. If there are is-a relationships in two classes, inheritance can be used, and inheritance is also a foreshadowing for implementing polymorphism. So what exactly is polymorphic? What is the implementation mechanism of polymorphism? In fact, the so-called polymorphic is a program in the same name method, but the function is different, allows objects of different classes to respond to the same message. It's one of the most important aspects of object-oriented.. polymorphism refers to the specific type of reference variable defined in the program and the method call emitted by the reference variable is not determined during programming, but is determined during the program's run, that is, a reference to which the variable is inverted to the instance object of the class that the reference variable emits, and the method called by which class is implemented. Must be determined by the time the program is running. Because when the program is run to determine the specific class, so that, without modifying the source code, you can bind the reference variable to a variety of different class implementations, resulting in the invocation of the specific method of the reference change, that is, do not modify the program code can be changed when the program runs the specific code, so that the program can select multiple running state, This is polymorphism. Polymorphism enhances the flexibility and extensibility of the software.  The popular explanation is that if there is a method called "Animal Cry" then, when you pass the parameter is animal dog, then call the dog cry, is the cat, then call the cat cry, this is polymorphic. Here are two examples of the images quoted: http://www.cnblogs.com/qinqinmeiren/archive/2011/07/15/2151687.html analogy:
You and your friends are heavy smokers,

One day, you didn't bring cigarettes, and your friend had n kinds of cigarettes,

You said: a cigarette,

A friend gives you a root;

You pass the smoke filter, the product is white sand smoke

You want one more,

The product is the Lotus King this time,

One more,

It's a panda this time.

...

The above description is polymorphic!

Smoke A = white sand
Smoke B = Lotus King
The smoke c = Panda ......What we are doing here is polymorphism.  White Sands, Lotus King, Panda are all smoke sub-class, we just through the smoke this one parent can refer to different subclasses, this is polymorphic-we can only run when we know the reference variable point to the specific instance object.  Yan a = new Baisha (); Here we understand that we define a Yan type of a, because Baisha is inherited Yan, so Baisha can be automatically up to Yan, so a can point to Baisha instances, so there is a strong advantage, Is that we know that the subclass is the extension of the parent class and has a more powerful function than the parent, if we have a reference to the parent class of the child class. Not only can you use the generality of subclasses, but you can also use the powerful features of subclasses. But the world is not perfect, the upward transformation there is a flaw, he will lead to some properties and methods lost. Cause we can't get them, so we define a reference to the parent class type to call properties and methods already defined in the parent class, and for properties and methods that exist only in subclasses 、、、

Another example of a chicken:
Chickens are the base class for all other chickens, defining a Method shape (), which can indicate the shape of a chicken. All sub-categories [Turkeys, pheasants, frogs, etc.] have this shape method, indicating their shape,

If I need a specific chicken in the following way,

[base class Reference = object of the implementation class]


For example:

Chicken A = new Turkey ();

Chicken A = new Frog ();

Chicken A = new pheasant ();

Chicken A = new hen ();



When I use it, I take a quote,

A.shape (), you will get the chicken form.


If I define a

Chicken A = new Turkey ();

So the a.shape get the form of a turkey, without the need for a specific

Turkey A = new Turkey (); And get the turkey form through A.shape.

----------------------------------------------------------------------------------A classic example of Java polymorphism: Citation:/http Blog.csdn.net/thinkghoster/article/details/2307001 class A ... {
Public String Show (D obj) ... {
Return ("A and D");
}
Public String Show (A obj) ... {
Return ("A and a");
}
}
Class B extends A ... {
Public String Show (B obj) ... {
Return ("B and B");
}
Public String Show (A obj) ... {
Return ("B and A");
}
}
Class C extends B ... {}
Class D extends B ... {}

(b) Question: What are the following output results?

A a1 = new A ();
A A2 = new B ();
b b = new B ();
c C = new C ();
D d = new D ();
System.out.println (A1.show (b)); ①
System.out.println (A1.show (c)); Ii
System.out.println (A1.show (d)); ③
System.out.println (A2.show (b)); ④
System.out.println (A2.show (c)); ⑤
System.out.println (A2.show (d)); ⑥
System.out.println (B.show (b)); ⑦
System.out.println (B.show (c)); ⑧
System.out.println (B.show (d)); ⑨

(c) The answer

             ①   A and A
             ②   A and a
              ③   A and D
               ④   B and A
              ⑤   B and A
              ⑥   A and D
              ⑦   B and B
              ⑧   B and B
              ⑨   A and D

①②③ better understand, generally will not be wrong. ④⑤ is a bit confused, why is not the output of "B and B" it?

First we need to know that when a superclass object reference variable references a subclass object, it is the type of the referenced object rather than the type of the reference variable that determines which member method to call, but the method that is called must be defined in the superclass, that is, the quilt class overrides the method. This statement is a generalization of polymorphism.

In fact, the invocation of an object method in the inheritance chain has a priority: This.show (O), Super.show (O), This.show ((Super) O), Super.show ((Super) O).

Following the above object method call precedence and we are not hard to find the reason here!

There are two main types of object polymorphism:

(1) The Java system is automatically completed when the subclass object is transformed to the parent class object.

(2) down transformation, when a parent object transforms into a subclass object, it must explicitly indicate the type of subclass to be transformed.

Java implementation polymorphism requires: (1) inherit (or give an interface implementation) (2) rewrite (3) The parent class's reference to the object of the subclass!!

I learn to summarize the use, if there are mistakes errors, please correct me, set an open-minded learning to correct! Thank you.

Do not find excuses for failure, only to find a way to success!!!

Java Fundamentals (iii)-----Polymorphism of three main features of Java object-oriented

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.