Consolidating Java (v)----Understanding Java Polymorphism by example

Source: Internet
Author: User

Package Duotai;class A{public String Show () {return "A";}} Class B extends A{public String Show () {return "B";} Public String Onlyb () {return ' Onlyb ';}} Class C extends B{public String Show () {return ' C ';} Public String Onlyc () {return ' ONLYC ';}} Class D{public string Show (String item) {return "d+" +item;} public int Show (int item) {return item;}} public class Main {public static void main (string[] args) {A A = new A (); b b = new B (); c C = new C ();  A ab = new B (); A ac = new C (); b BC =new C ();/* B is a subclass of a, C is a subclass of B, a method show () in A, and B and C are all rewritten. * Overrides are the representation of the parent class and the subclass polymorphism, the object called by the subclass will call this method defined in the subclass, and this method in the parent class is masked.      * The method is called by the A,b,c object, so the results are a,b,c * *///1SYSTEM.OUT.PRINTLN ("1:a.show () =" +a.show ()).      Output: 1:a.show () =a//2system.out.println ("2:b.show () =" +b.show ());      Output: 2:a.show () =b//3system.out.println ("3:c.show () =" +c.show ()); Output: 3:a.show () =c/* * When a superclass invokes an object of its subclass by declaring a reference variable (which is more appropriate to refer to it as a reference to an object), the method it calls must be a method of the subclass owned in the superclass. * 4 the variable AB is declared by a, refers to the object of B, so Ab.show () is called the Show () method in Class B, but if the use of the AB variable call ONLYB () * is not adjustable, because the method is not in the superclass. * 5, 6 with 4 * *///4 System.out.println ("4:ab.show () =" +ab.show ());    Output: 4:ab.show () =b//5 System.out.println ("5:ac.show () =" +ac.show ());    Output: 5:ac.show () =c//6 System.out.println ("6:bc.show () =" +bc.show ());         Output: 6:bc.show () =c/* * There are two show methods in D, but their parameters and return values are different, which is an overload of the method.         * Overloading is a representation of polymorphism in a class with the same method name, but the number of parameters, the type, and the return value are at least one different.        * */d D =new D ();   7 System.out.println ("7:d.show (ITEMSTR) =" +d.show ("DD"));      7:d.show (ITEMSTR) =d+dd//8 System.out.println ("8:d.show (itemint) =" +d.show (1)); 8:d.show (Itemint) =1}}

Consolidating Java (v)----Understanding Java Polymorphism by example

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.