JAVA 8 polymorphic

Source: Internet
Author: User

An object-oriented third feature, polymorphic:

  can be understood as the various states in which things exist.  1, polymorphic embodiment: a reference to the parent class points to its own subclass, and a reference to the parent class can receive the subclass object. Animal C1 = new Cat ();  c1.eat ();  function (new Dog ());  function (new Pig ());   public Static Voi D function (Animal c)  {  c.eat ()  }  2, polymorphic premises         must be a relationship between classes and classes, or inheritance, It's either implementation.         There is usually a prerequisite, there is coverage. 3, polymorphic applications  4, polymorphism benefits         dramatically improved program extensibility  5, drawbacks         improved scalability, However, members in the parent class can only be accessed using a reference to the parent class.   Example program:  class zx{ public static void Main (String []arr)  {   Animal c1 = new Cat ();  C1. Eat ();  function (new Dog ());  function (new Pig ());  }   public static void function ( Animal c)  {  c.eat ()  }} abstract class animal{ public abstract void Eat ();}  class Cat extends animal{ public void eat ()  {  System.out.println ("Eat fish");  } public void Catchmouse ()  {  System.out.println ("Catch Mouse");  }  }&nbsP;class Dog extends animal{ public void eat ()  {  System.out.println ("Bone");  } public void Kanjia ()  {  System.out.println ("See");  }}  class Pig extends animal{ public void eat ()  {  System.out.println ("feed");  } public void Gongdi ()  {  System.out.println ("Public land");  }}   before modifying  class zx{ public static void Main (String []arr)  {  Cat c = new Cat ();   Cat C1 = new Cat ();  function (c);  function (new Dog ());  function (new Pig ());  } public St atic void function (Cat c)  {  c.eat ()  }  public static void function (Dog c)  {  C.eat ();  }  public static void function (Pig c)  {  c.eat ()  }} abstract class animal{  public abstract void Eat ();}  class Cat extends animal{ public void eat ()  {  System.out.println ("Eat fish");  } public void Catchmouse ()  {  System.out. println ("Catch Mouse");  }  } class Dog extends animal{ public void eat ()  {  System.out.println ("Bones");  } public void Kanjia ()  {  System.out.println ("See"); }}   class Pig extends animal{ public void eat ()  {  System.out.println ("feed");  } public void Gongdi ()  {  System.out.println ("commons"); }}    requirements:  animal a= new Cat ();  a.eat ( ); To invoke the unique method of the cat, force the reference of the parent class to the type of the subclass and move down. Cat C = (cat) a;c.catch.mouse;  but can not Animal a = new Animal (); Cat C = (cat) A; Polymorphism is changing from beginning to finish.    keyword instanceof: Determines whether the object's reference points to a type  cat m = new Cat ();  m instanceof Cat;   Truea  instanceof Dog; Characteristics of member functions (non-static) in false   polymorphism:  at compile time: see if there are methods called in the class to which the reference type belongs, and if so, the compilation passes, otherwise it fails. At run time: The Lookup object belongs to the class in which there is a method called. The member function in the polymorphic call is, compile look left, run look to the right.   in polymorphism, member variables (and static methods) feature: Regardless of compilation or run, member variables are referred to the left. Examples of applications for       polymorphism:   class zx{ public static void Main (String []arr)  {    mainboard MB = new mainboard ();  Mb.ran ();  mb.usepci (null);  Mb.usepci (New netcard ());  Mb.usepci (New Mucard ());   }} class mainboard{ public Void Ran ()  {  System.out.println ("Main board ran");   } public void Usepci (PCI P)  /* interface type refers to its own subclass object, interface cannot be created object, one of the polymorphic applications,  Improved program extensibility, reduced coupling */ {  if (p!=null)   {   p.open ();   p.close (); }   }   } interface pci{ public Void Open ();  public void Close ();  }class Netcard implements Pci{ public void Open ()  {  SYSTEM.OUT.PRINTLN ("net Open");  } public void Close ()  {  System.out.println ("net Close");  }} class Mucard implements pci{ public Void Open ()  {  System.out.println ("Mu open");  }  public void Close ()  {  System.out.println ("Mu close"); }}      extensibility Examples:   Requirements: Database operation  1, connect database 2, operate database 3, close database connection    write below Userinfodao UI = new Userinfobyjdbc (); Can    Object class:is the direct or indirect parent class for all objects. What is defined in this class is the functionality that all objects have. Class Demo//default extends object{} object.squals (Object obj); The return value is true or FALSE when both address values are the same as true object.tostring ();//Returns the character of the object that returns the "class name @ Address value" Address value of 16 Object.hanshcode//returns int decimal Address value

JAVA 8 polymorphic

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.