Override of methods in Java

Source: Internet
Author: User

Override of the so-called method is that the subclass defines a method with the same name in the parent class, but must consider the permission when the method is overridden, that is, the quilt-class override method cannot have more restrictive access than the parent class method.

Modifiers are public, protected, default, private, respectively. Their access rights are as shown in.

Classes that are decorated with public are allowed to access each other in this class, in the same package, in subclasses, and in other packages.

Classes decorated with protected are allowed to access each other in this class, in the same package, in subclasses, and protected cannot be accessed from one another in a class that does not have an inheritance relationship outside the package.

It is possible to show that classes with the default modifier are accessible to each other in this class, in the same package, and that default cannot be accessed outside the package, regardless of whether the class has an inheritance relationship.

Classes that are decorated with private can only be accessed in this class.

Their access size is public > protected > Default > Private;

If you use public-defined methods in the parent class, the access permissions in the subclass must be public, or the program cannot compile.

Class Person{void print () {System.out.println ("person-to-void print () {}");}} Class Student extends person{public void print () {                                    System.out.println ("Student-to-void print () {}");  Overrides the method in the parent class, extending the permissions}}public class OverrideDemo01 {public static void main (string[] args) {new Student (). print ();}}

  

The private method defined in the parent class is defined in the subclass as the default method, which is not a method override.

Class person{private void print () {System.out.println ("person-to-void print () {}");} public void Fun () {this.print ();}} Class Student extends Person{void print () {System.out.println ("Student-to-void print () {}");}} public class OverrideDemo04 {public static void main (string[] args) {new Student (). Fun ();}}
Output: Person-to-void print () {} Visible call method is the method of the parent class, that is, the subclass does not overwrite the method in the parent class, but a new method is redefined in the subclass.


If the same attribute is declared in the parent class subclass, it is necessary to use the "closest access principle" when accessing the subclass directly, that is, to find the properties in this class, and if you want to invoke the properties in the parent class, use the Super. Properties format directly.

The difference between overwrite and overloading

Override of methods in Java

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.