Java Overwrite (Override) detailed __java

Source: Internet
Author: User

first, the concept of overwrite

Now that there is an inherited relationship, there is a link between the subclass and the parent class, and in the subclass it is possible to define the name of the method or property that is exactly the same as the parent class, which is called a overwrite.

second, the method of the review

When a subclass defines a method that is identical to the parent class in the method name, return value type, parameter type, and number, it is called a method override.

No overwrite action:

Package Com.wz.overridedemo;

Class A {public
    void print () {
        System.out.println ("Hello world.");
    }

Class B extends A {
} public

class Test {public
    static void Main (String args[]) {
        b = new B ();
        B.print (); Method inherits from the parent class
    }

Run Result:

Hello World.

The subclass object is instantiated, but there is no print () method in the subclass, then the PRITN () method inherited from the parent class is used.

Implementation override:

Package Com.wz.overridedemo;

Class A {public
    void print () {
        System.out.println ("Hello world.");
    }

Class B extends A {public
    void print () {///method name, parameter type and number, return value all the same
        System.out.println ("World, Hello.") ");
    }
}

public class Test {public
    static void Main (String args[]) {
        b = b = new B ();
        B.print (); Method inherits from the parent class
    }

Run Result:

World, hello.

when a method in a class is overwritten, the method invoked is the overridden method if the subclass object is instantiated.

One thing to note when doing a method overwrite is that the method overridden by the quilt class cannot have more restrictive access control permissions than the parent class.
If the method in the parent class is a default permission, the subclass can overwrite only the default or public permission, and if the parent class's method is public, then the access permission for the method in the subclass can only be public.

When a subclass has overridden a parent class method, the subclass wants to invoke the overridden method of the parent class, adding "super" before the method:

Package Com.wz.overridedemo;

Class A {public
    void print () {
        System.out.println ("Hello world.");
    }

Class B extends A {public
    void print () {
        super.print ();
        System.out.println ("The World, Hello.") ");
    }
}

public class Test {public
    static void Main (String args[]) {
        b = b = new B ();
        B.print (); Method inherits from the parent class
    }

Run Result:

Hello World.
World, hello.

Be sure to remember the scope of operation:
(1) this. Method (): First find out if a specified method exists, or invoke the parent action if it is not found;
(2) Super. Method (): Call the specified method in the parent class directly from the subclass, and no longer find the subclass.

Question: Will the following actions be overridden?

Package Com.wz.overridedemo;

Class A {
    private void print () {
        System.out.println ("Hello world.");
    }

    public void Fun () {
        this.print ();
    }
}

Class B extends A {public
    void print () {//not called Overwrite
        System.out.println ("World, Hello.") ");
    }
}

public class Test {public
    static void Main (String args[]) {
        b = b = new B ();
        B.fun (); Method inherits from the parent class
    }

Run Result:

Hello World.

First of all, from the concept of overwrite: Now the permissions of the parent class is private, and the subclass is public, it does extend the permissions, and the method's parameter name and number, return value type are the same. However, this situation will not occur in the development of, the use of private defined operations can not be overridden.

Iii. The difference between this and super

the difference between method overload and overwrite

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.