Java polymorphic---ABC case list

Source: Internet
Author: User

  

classa{ Public voidShow () {Show2 (); }     Public voidShow2 () {System.out.println (I); }}classBextendsA {/*Public Void Show () {Show2 (); }    */     Public voidShow2 () {System.out.println (Love); }}classCextendsb{ Public voidShow () {Super. Show (); }     Public voidShow2 () {System.out.println (You); }} Public classduotaitest4{ Public Static voidMain (String[]args) {A A=NewB ();                A.show (); b b=NewC ();    B.show (); }}

Summarize:

Features of polymorphic member access:

Method: Compile to see left, run to see right.

When inheriting:

Subclasses have the same method as in the parent class, called Overrides.

There are no methods in the subclass that have occurred in the parent class, and the method is inherited.

A.show ();

At compile time, look to the left, A has the show () method, compile without error, run to run the Show method in B.

(b Although did not write the show method, but in fact, the show in A has inherited!)! )

B.show ();

Run look to the right:

public void Show () {     Super. Show ();    }
       and Super calls the Show method in the parent class B, and B in Show calls Show2 (), although B also has show2 (), but don't forget, we actually create the object of C, (new C ();), the last run to see the right, Don't forget it.
              
public void Show () {      show2 ();    }                  
          
Above.

Java polymorphic---ABC case list

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.