Polymorphism in Java, conversion of reference types

Source: Internet
Author: User

1. Polymorphism is divided into reference polymorphism and method polymorphism, see Test class

     Package COM.WANGCF;     // Parent Class     Public class Animal {        publicvoid  eat () {            System.out.println ("Method of the parent class")  );        }    }
 package   COM.WANGCF;  //  subclass  public  class  Bridge extends   animal{ public  void   eat () {    System.out.println ( "sub-class birds override method"  public  void   Fly () {System.out.println (" Subclass birds have no way to fly in the parent class there is no "
 Package COM.WANGCF; // sub-class  Public class extends animal{    }
 PackageCOM.WANGCF;//Test Class Public classTest { Public Static voidMain (string[] args) {//1. Quoting polymorphicAnimal obj1=NewAnimal ();//references to the parent class can point to objects of this classAnimal obj2=NewBridge ();//a reference to the parent class can point to the object of the child classAnimal obj3=NewTiger (); Tiger Obj4=NewTiger (); //bridg obj3=new Animal (); A subclass reference cannot point to the parent class//2. Methods the method of the subclass is preferred, and if the subclass is not overridden, the method of the parent class is used; the parent class reference cannot call a method unique to the subclassObj1.eat ();//create this class object of the parent class, calling methods of this class when invoking a methodObj2.eat ();//if the parent class points to a subclass object, the calling method is the overriding method of the subclassObj3.eat ();//or the Inheritance methodObj4.eat ();//If you create a subclass of this class object without overriding the method of the parent class, the method of the parent class is called//Obj2.fly (); A method unique to a subclass cannot be called by a reference to a parent class                    }}

2. Conversion of reference types

(1) Upward transformation, small type conversion to large type, it is implicit, automatic, there is no hidden danger

(2) Downward transformation, large type conversion to small type, it is cast, there is a hidden danger, the parameter memory overflow; You can use the INSTANCEOF operator class to avoid security issues

 PackageCOM.WANGCF;//Test Class Public classTest { Public Static voidMain (string[] args) {Bridge bri=NewBridge (); Animal Animal=bri;//Upward Transformation//Bridge bri2=animal; conversion down, error requires use of castBridge bri1= (bridge) animal;//forcing//Tiger tig= (Tiger) animal;//Although the compiler did not error, the runtime will be wrong, we know that animal is briger conversion, it can not be converted to Tiger//Tiger tiger= (Tiger) BRI; //cannot cast from Bridge to Tiger//use instanceof to judge        if(AnimalinstanceofTiger) {Tiger TIG1=(Tiger) animal; }Else{System.out.println ("Unable to type conversion"); }    }}

Polymorphism in Java, conversion of reference types

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.