"Introduction to Programming (Java) & #183; 2.1.3 Rewriting (override)"

Source: Internet
Author: User

Introduction to Programming (Java) 2.1.3 Overwrite (override) to collect the override content.

method overriding refers to an instance method defined for a parent class that agrees that a subclass provides a language attribute of its own implementation. Implementations of subclasses will overwrite (override) or replace (replace) the implementation in the parent class.

The following conditions must be met at "the same time" for rewrite success.

    • The method signatures of both are identical.
A method signature (signature) refers to a method's name and a list of parameters. A method ' s signature is its name and the number and the type of its arguments. The method signatures of overloaded methods must be different. Essentially. A different method of signing is two different methods.


    • The return type must be the same or a subclass. Before Java 5.0. A different return type will cause a compilation error. After Java 5.0, the return type of the overridden method can be a subclass (the return type of the overridden method).

      Covariant return type

    • When the method is overwritten, access permissions cannot become more private. The same is not required for both access modifiers, but the overridden method can be modified with the package private, protected, and public, assuming that the method of the parent class is a private package. Assuming that the method of the parent class is public, the overridden method can only be modified with public.
    • The overridden method cannot throw many other check-type exceptions than the parent class method.
Public interface sup{public       void Copy ();   Class Sub implements sup{    @Override public void Copy () {           System.out.println ("Override");         throw new NullPointerException ();    
Compiler regardless of runtimeexception.


    • The method of the parent class is not the final method. The final method cannot be rewritten.
    • The method of the parent class is not a private or static method. Otherwise, even if the signature is written in exactly the same way. But they have nothing to do with the rewriting mechanism .

The private method of the parent class. Subclasses do not inherit. Thus, a subclass is meaningless, as if it does not exist.

The static method of the parent class, foo (), is assumed to be inheritable. Subclasses can write method signatures with the same static method as the requirements for rewriting, such as access to public, return value types, and so on. But. The front can not add @override.

There is a juxtaposition of both. Method Hide: Call foo () directly in the subclass to run the custom static method. Assuming that there is no custom foo ()--Just a static method foo () that inherits from the parent class, call Foo () directly in the subclass and obviously run a unique foo ().

So. A custom static method blocks the method of the parent class.


Under the premise of being able to inherit:

The instance method of the parent class. Subclasses can override an instance method , but the subclass must not define the same static method of the signature-compilation error.


A static method of a parent class that can write a static method of concealment, but the subclass must not define the same instance method of the signature-the compilation error.

overriding and overloading

As compared to boys and girls. Rewriting is an execution-time mechanism, and overloading is a compile-time mechanism. Only read the source code when there is a comparative value, caustic to say, only the test can have a comparative value.



"Introduction to Programming (Java) & #183; 2.1.3 Rewriting (override)"

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.