15. Method Rewrite rules

Source: Internet
Author: User

 Public classBase { Public voidmethod1 () {System.out.println ("Instance method of the parent class"); }     Public Static voidmethod2 () {System.out.println ("Static method of the parent class"); }     PublicBase method3 () {System.out.println ("Parent class returns a method with a value type of base"); return NewBase (); }        Private voidmethod4 () {System.out.println ("Private method of the parent class"); }}
 Public classSubextendsbase{//private void Method1 () {//access permissions cannot be strict with the parent class//Public static void Method1 () {//a non-static method of a parent class cannot overwrite a quilt class as a static method     Public voidmethod1 () {System.out.println ("Instance methods for subclasses"); }        //Public void Method2 () {//static methods of a parent class cannot be overridden by a quilt class as a non-static method//subclasses can define static methods that have the same name as the parent class, so that the static methods of the parent class are "hidden" in the child class     Public Static voidmethod2 () {System.out.println ("Static methods for subclasses"); }        //The return value type is the same or its child class     PublicSub method3 () {System.out.println ("Subclass returns a value of sub method"); return NewSub (); }        //the private method of the parent class cannot be overridden by the quilt class, so it can be written, but it is a standalone method     Public voidmethod4 () {System.out.println ("Private Methods of subclasses"); }}

15. Method Rewrite rules

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.