Java up and down transformation 1

Source: Internet
Author: User

In the Java inheritance system, it is considered that the parent class (superclass) is on the upper level, the subclass is in the lower layer (derived class), and the upward transformation is to turn the object into the parent class object.

1  Public class Father {    2public     void  eat () {3         System.out.println ("I am the method of the parent class to eat ....") "); 4     }5 }
1  Public classSonextendsfather{2     3      Public voideat () {4System.out.println ("I am son's method to eat .... ");5     }6      Public voidsleep () {7System.out.println ("Son sleeps");8     }9}

Test

1  Public Static voidMain (string[] args) {2Father f=NewSon ();//sub-class upward transformation3Person p=NewSon ();//the normal new4 f.eat (); 5             //f.sleep (); Cannot call this method this method does not exist6 p.eat ();7 p.sleep ();8     9         Ten     }     One running Result: I'm a subclass of methods to eat A I'm a subclass of methods to eat -Go to bed

F.sleep () cannot be called here, which means that a method that is not defined in the parent class is lost after the upward transformation, and only methods defined in the parent class can be called. So what's the use of upward transformation? Why not just call directly that subclass's own defined object?

If the direct new Son () call can also not reflect the object-oriented abstract programming ideas, and the code extensibility is poor.

If the parent class has a subclass Son1

1  Public classSon1extendsFather {2      Public voideat () {3System.out.println ("The Eat Method of Son1");4     }5      Public voidsleep () {6System.out.println ("Son1 's Sleep Method");7     }8 9}

See Test code below

1  Public classTest {2      Public Static voidMain (string[] args) {3        4ttNewSon ());5ttNewSon1 ());6     }7     8     9      Public Static voidtt (son son) {Ten          One son.eat (); A     } -      Public Static voidtt (Son1 son) { -          the son.eat (); -     } -  -}

My TT method calls for some or all of the methods of each subclass, as shown above, and if I have a lot of subclasses, the code will be particularly heavy on the repetition rate.

An upward transformation can be a perfect solution to this kind of problem: no matter how many subcategories I have, it can be done in a single way.

    

1  Public classTest {2      Public Static voidMain (string[] args) {3        4ttNewSon ());5ttNewSon1 ());6     }7     8     9      Public Static voidtt (Father son) {Ten         //parent class object as instantiated parameter One son.eat (); A     } -}

    

Java up and down transformation 1

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.