Further understanding of polymorphic concepts in Java _java

Source: Internet
Author: User

There are two types of polymorphism:
1) Compile-time polymorphism
For multiple methods with the same name, it is called compile-time polymorphism at compile time to determine which of the methods to execute the same name.
2 Run-time polymorphism
If you are unsure at compile time, you can only determine which of multiple methods of the same name executes at run time, which is called Run-time polymorphism.

Method overlay shows two polymorphism, which is compile-time polymorphism when an object obtains this class instance, otherwise, it is run-time polymorphism, for example:
XXXX x1 = new xxxx (parameter list); object to an instance of this class, and the object is the same as the instance type it references
xxx xx1 = new xxx (parameter list);
X1.tostring (); Compile-time polymorphism, executing the method of the XXX class.
Xx1.tostring (); Compile-time polymorphism, which performs the method of XXXX class overlay.
XXXX is the parent class for XXX.
Because the subclass object is both a parent object and the parent class object has an assignment compatibility with the subclass object, the parent class object can be assigned as a subclass object. For example,
XXXX x2 = new XXX (parameter list); The parent class object obtains the subclass instance and the subclass object is the parent class object
X2.tostring (); Run-time polymorphism

X2 declare as a parent object but get an instance of subclass XXX, then x2.tostring () actually executes the parent class method or executes the subclass override method?
This is divided into two situations:
Depends on whether the subclass overrides the parent class method. If the subclass overrides the parent class method, the subclass method is executed;
If not overridden, the parent class method is executed.
At compile time, depending on the class that the object belongs to, the system cannot determine exactly how the class should be executed, only the runtime can determine it, so this is run-time polymorphism.
The parent class object cannot execute all subclass methods, only subclass methods that are declared in the parent class that are overridden by subclasses.

Java Multi-State implementation
Java polymorphism, like C + +, is implemented by dynamically binding or run-time binding. When a method referenced by an object is invoked, the compiler does not know whether the reference refers to the type object that is described in the variable declaration, or the object of the subclass of that type. Therefore, the compiler cannot bind to a specific method for this call. Bind to specific methods at run time only through runtime type recognition (RTT) in Java

The overridden overriding of methods and the overloaded overloading of methods are different manifestations of Java polymorphism. Overriding overriding is a manifestation of polymorphism between parent classes and subclasses, and overload overloading is the expression of polymorphism in a class.

Give a concrete example:

 class People {public 
  String toString () {return 
   "I am a people!"; 
  } 
  
  public void Eat () { 
  }; 
  
  public void Speak () { 
  }; 
 } 
  
 Class Boy extends people {public 
  String toString () {return 
   "I am a boy!"; 
  } 
  
  public void Fight () { 
  }; 
  
  public void Speak () { 
  }; 
 } 
  
 Class Girl extends people {public 
  String toString () {return 
   "I am a girl!"; 
  } 
  
  public void Sing () { 
  }; 
  
  public void Speak () { 
  }; 
 } 
  
 public class Testtostring {public 
  static void Main (String args[]) { 
   people p = new Girl (); 
   System.out.println (P.tostring ()); 
  } 
  

The results of the operation are:

I am a girl!

P is a reference to people, but at run time because it is a girl object, it still invokes the girl ToString method


deep understanding of Java polymorphism
statement, here for reference to other students examples, the original link: http://blog.csdn.net/thinkghoster/article/details/2307001

Test topics

 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 { 
   
 } public 
  
 class Main {public 
  static void main (String ar Gs[]) { 
   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)); 1 
   System.out.println (A1.show (c));//2 
   System.out.println (A1.show (d));//3 
   System.out.println ( A2.show (b)); 4 
   System.out.println (A2.show (c));//5 
   System.out.println (A2.show (d));//6 
   System.out.println ( B.show (b)); 7 
   System.out.println (B.show (c));//8 
   System.out.println (B.show (d));//9 
  } 
 } 

Answer

 A and a A and A and D B and a B and a 
 a 
 and D B and B 

Analytical
I started to do this topic, 4, 5, 6, 9 all wrong, the reason is not very good understanding of Java polymorphism, here to explain

First, to have a deep understanding of overrides and overloads, overrides include not only the same function name, but also the parameter type and return value type

Second, a profound understanding of this sentence "When a superclass object references a variable referencing a subclass object, the type of the referenced object, rather than the type of the reference variable, determines whose member method to invoke, but the invoked method must be defined in the superclass, that is, the quilt class overrides the method"

And then, we're going to analyze these questions.

Question: Do you think B rewrites the show method of the parent class A? If overridden, how many are rewritten?

Answer: Rewrite, rewrite one, that is, public string show (a obj), why is public string show (B obj) not a rewrite of the parent class method, very simple because the parameter types are different

Example analysis
looking at the above analysis, we also analyze two examples:

I. A2.show (b):

A2 is a reference variable, and the type a,b is an instance of B. First, find show (B obj) in Class A, not found. So we look in the superclass of a, and a doesn't have a super class, so it turns to A.this (super) B, (super) B is a, so a method of show (a obj) is found in a, but because of an object of Class B referenced by A2, b rewrites the show (a obj) method of a. So the end is locked to show (a obj) of Class B, and the output is "B and a"

II. A2.show (c):
A2 is a reference variable, and the type a,b is an instance of B. First, find show (C obj) in Class A, not found. So go to the super class of a, and a does not have a superclass, so it turns to A.this (super) C, (super) B, and so far this a2.show (c) becomes the problem of A2.show (b), and a2.show (b) above has analyzed the output "B" And a "so this is also the output" B and a "


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.