Object-oriented (polymorphic downward transformation) of the first season of Java entry

Source: Internet
Author: User

Last blog (http://blog.csdn.net/qq_32059827/article/details/51328638) finally on the shortcomings of the polymorphic show, here to solve this drawback. As follows:

/* polymorphic Disadvantages: You cannot use the unique features of subclasses. I just want to use the special features of subclasses, okay? How to use it? A: You can call A method to create A subclass object. (yes, but a lot of the time is unreasonable.) And, too much memory) B: casts a reference to the parent class into a reference to the subclass. Transformation between objects (downward transition): upward transformation: Fu f = new Zi (); downward transformation: (prone to problems) Zi Z = (Zi) F; Requires that the F must be capable of being converted to Zi. Type to match the non-row case: Ren r = (cat) cat (); it's obviously impossible for a cat to turn into an adult. */class Fu {public void Show () {System.out.println ("show Fu");}} Class Zi extends Fu {public void Show () {System.out.println ("show Zi");} public void Method () {System.out.println ("method Zi");}} Class DuoTaiDemo4 {public static void main (string[] args) {//Test fu F = new Zi (); F.show ();//f.method ();//no symbol found, no method in parent class ( //Create subclass object, yes, but many times unreasonable. And, too much memory//zi z = new Zi ();//z.show ();//z.method (); Zi z = (Zi) f;//Note Format Oh! Z.show (); Z.method ();}}

Now that there is a problem with the downward transition, the next step is to demonstrate a problem with the downward transition. (This problem is due to grammatical nonstandard)

/*classcastexception: Type conversion exceptions are generally easy to appear in a polymorphic downward transition */class Animal {public void Eat () {}}class Dog extends Animal {public void Eat ( {}public void Lookdoor () {}}class Cat extends Animal {public void Eat () {}public void PlayGame () {}}class DuoTaiDemo5 {p Ublic static void Main (string[] args) {//In memory is the dog animal a = new dog (); System.out.println (a);D og d = (Dog) a;//downward transformation System.out.println (d);//Memory is cat//animal a = new Cat (); Animal A remember that you cannot define this. ERROR: Variable A has been defined in method main (string[]). Personal understanding is the occurrence of variable assignment "conflict"//(the reader wants to see the specific reasons for the above error) A = new Cat ();//Downward Transition System.out.println (a); Cat C = (cat) a;//downward Transformation System.out.println (c);//memory is cat//dog dd = (Dog) A; Classcastexception,a is the cat's content and cannot be transformed downward to the dog. A cat becomes a dog.}

The memory plots for polymorphic problems are as follows:


Object-oriented (polymorphic downward transformation) of the first season of Java entry

Related Article

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.