"The Polymorphism of Java"

Source: Internet
Author: User

Java Polymorphism is one of the three main features of object-oriented. (Encapsulation, inheritance, polymorphism), which refers to the specific type of reference variable defined in the program and the method call made by the reference variable are not deterministic during programming, but are determined during the program's run, that is, the instance object of the class to which a reference variable is inverted. The method call that the reference variable emits is exactly the method that is implemented in the class that must be determined by the time the program is run. 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.



Java polymorphism is divided into:


1. Overloads and overrides of the method.


Overloading: Two methods with the same return value and function name in the same class, and a different argument list.


Overriding: A method of a subclass that overrides a method of a parent class that requires the same method name and parameters


2. The polymorphism of the object

Here we mainly introduce the polymorphism of the object


My own understanding is that small turn large automatic conversion, large turn small need to cast. Parent class is greater than child class.

(1) Parent class reference to child class instance

  1. public class Mystring {


  2. public static void Main (string[] args) {


  3. Parent class is large, sub-class small, small turn large automatic conversion, can only call the parent class also has the method

  4. Person temp=new student ();//reference to parent class to child class object

  5. Temp.say ();


  6. Person temp1=new teacher ();

  7. Temp1.say ();

  8. }


  9. }

  10. Abstract class

  11. Abstract class Person

  12. {

  13. public String name;



  14. Public String GetName () {

  15. return name;

  16. }



  17. public void SetName (String name) {

  18. THIS.name = name;

  19. }


  20. public abstract void Say ();//abstract method

  21. }

  22. Student class

  23. Class student extends person//inherit person class

  24. {

  25. Implementing an abstract method

  26. public void Say ()

  27. {

  28. System.out.println ("I am a student ....") ");


  29. }

  30. }

  31. Teacher Class

  32. Class teacher extends person//inherit person class

  33. {


  34. public void Say ()

  35. {

  36. System.out.println ("I am a teacher ....") ");


  37. }

  38. Extension methods

  39. public void SayHello ()

  40. {

  41. System.out.println ("Hello, I am a teacher ....") ");

  42. }

  43. }

Copy Code

650) this.width=650; "id=" aimg_288 "src=" http://techfoxbbs.com/data/attachment/forum/201505/21/ 110413br5n9255r2fnk889.png "class=" Zoom "width=" 274 "alt=" 110413br5n9255r2fnk889.png "/>
(2) The subclass object points to the parent class reference
  1. public class Mystring {


  2. public static void Main (string[] args) {


  3. The parent class is large, the sub-class is small, auto-converted, and only the parent class can call methods

  4. Person temp=new student ();//reference to parent class to child class object

  5. Temp.say ();


  6. Person temp1=new teacher ();

  7. Temp1.say ();

  8. Extension method for------------call teacher SayHello ()

  9. The parent class person does not have a SayHello method and cannot directly Temp1.sayhello ()


  10. Child class is less than parent class, requires casting

  11. Teacher temp3= (teacher) Temp1;

  12. Temp3.sayhello ();


  13. }


  14. }

  15. Definition of omitted class

Copy Code

Execution Result:650) this.width=650; "id=" aimg_289 "src=" http://techfoxbbs.com/data/attachment/forum/201505/21/ 110443w77q858wvwrr4067.png "class=" Zoom "width=" 313 "alt=" 110443w77q858wvwrr4067.png "/>

I think remember small turn large automatic conversion, large turn small need to cast. The parent class is better understood than the subclass


Since the parent class can be automatically converted to subclasses, we can use this feature to solve a problem that is often encountered

  1. public class Mystring {

  2. public static void Main (string[] args) {

  3. Student temp=new student ();

  4. Say (temp);

  5. Teacher Temp1=new teacher ();

  6. Say (TEMP1);

  7. }

  8. public static void Say (Student temp)

  9. {

  10. Temp.say ();

  11. }


  12. public static void Say (teacher Temp1)

  13. {

  14. Temp1.say ();

  15. }

  16. Definition of omitted class

  17. }

Copy Code

we found that say () overloaded two parts due to the different parameters required. In this case, if the class is many, do not overload a lot of copies. Here we use the polymorphic of the object to be able to solve very well. Change the code to:
  1. public class Mystring {


  2. public static void Main (string[] args) {


  3. Person temp=new student ();

  4. Say (temp);


  5. Person temp1=new teacher ();

  6. Say (TEMP1);



  7. }

  8. public static void Say (person temp)

  9. {

  10. Temp.say ();

  11. }

  12. Definition of omitted class

  13. }

Copy Code

Of course, the method of invocation can only be used by the parent class, and this is because the parent class also has the Say () method. To solve this problem, we introduced the keyword instanceof.

The basic syntax for instanceof is: Object instanceof type-----------Returns a Boolean value

To determine whether an object belongs to an instance of a class, returns true, otherwise false


Below we call the teacher extension method SayHello () in the Say () method. Both the student and the person classes do not use this method, and the code above is definitely an error. This is where we can change the code to:

  1. public class Mystring {


  2. public static void Main (string[] args) {


  3. Person temp=new student ();

  4. Say (temp);


  5. Person temp1=new teacher ();

  6. Say (TEMP1);



  7. }

  8. public static void Say (person temp)

  9. {

  10. Cast to teacher if it is a teacher object (big turn small)

  11. if (temp instanceof teacher)

  12. {

  13. Teacher ok= (teacher) temp;

  14. Ok.sayhello ();

  15. }


  16. }



  17. Definition of omitted class

  18. }

Copy Code

Parent class Big turn small automatic conversion, reminds me of all classes of the parent classObjectclass, of course it is the biggest. All of our code can also be changed to:
  1. public class Mystring {


  2. public static void Main (string[] args) {


  3. Person temp=new student ();

  4. Say (temp);


  5. Person temp1=new teacher ();

  6. Say (TEMP1);



  7. }

  8. Using the object class

  9. public static void Say (Object temp)

  10. {

  11. Cast to teacher if it is a teacher object (big turn small)

  12. if (temp instanceof teacher)

  13. {

  14. Teacher ok= (teacher) temp;

  15. Ok.sayhello ();

  16. }


  17. }



  18. Definition of omitted class

  19. }

  20. Abstract class

Copy Code

This blog is a summary of my study of Java polymorphism, but also come out and share with you, there is nothing wrong still look.


"The Polymorphism of Java"

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.