Java Handy Note three polymorphism

Source: Internet
Author: User

Polymorphism:---The various morphological essence of an object: the reference of the parent class points to the subclass object---Premise: there must be an inheritance or implement class Animal{}class dog extends Animal{}main{dog d = new Dog ();//Native Animal dog = new Dog (); Polymorphic}  Note: objects created by polymorphic forms can only access member methods that are common to the child parent class, and the result is a subclass-specific member method that cannot be used by the child class. Virtual method Invocation: The parent class object calls the subclass-specific method Student S = new Student ("Zhang San", 100); Person p = new Student ("John Doe", 200); System.out.println (S.say ()); System.out.println (P.say ());  polymorphic array-In an array of reference types, the object is stored in polymorphic form. Eg:person[] person = {New Person ("Zhang San", +), new Student ("John Doe", +, +, 90.0), New Student ("Harry", 119, 91.5), New Teacher (" Ms. Liu ", (+)," Java EE "), New Teacher (" Mr. Zhang ")};  polymorphic Parameters-the reference type parameter in the method parameter list instanceof operator-Determines whether an object is coerced type conversion for a specified type (pattern)-objects from One of the reference patterns is converted to another reference morphological Object association: One object is used with another object. Single-to-many, many-to-one class Test4 {public static void main (string[] args) { person p = new Person ("Harry", "N"); method (P); Student s = new Student ("Zhang San", +); method (s); person P1 = new Student ("Zhao Si", "n"); method (p1);}  public static void Method (person p) {if (P instanceof Student) {System.out.println ("teacher"); Student s = (Student) p; System.out.println ("state method invocation:"+p.say ());} Else{system.out.println ("person");}  system.out.println (P.say ());}}  class person{private string name;private int age; public person () {} public person (String Name,int age) { THIS.name = Name;this.age = age;}  public String GetName () {return name;}  public String Say () {return "name:" +name+ "Age:" +age;}}  class Student extends person{private int id;private double score; public Student (String name,int ID) {This ( name,20,id,98.5);}  public Student (String name,int age,int id,double score) {super (name,age); this.id = Id;this.score = score;}  public Double getId () {return ID;}  public String Say () {return Super.say () + "study Number:" +id+ "score:" +score;}}

Java Handy Note three 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.