What is rewriting in Java?

Source: Internet
Author: User

Override (Override)

Like what

Defining the Father Class

1: Name, meal method, eat can窝窝头pickles.

2: Define son class, inherit father

The 1:son class does not define any members, the subclass creates the object, and the method of eating can still be called.

2: Father's way of eating, son does not want to eat. Son himself defined the method of eating.

1: There is a way to eat in the parent class, there are 2 ways to eat in the subclass, exactly the same, but the method body is not the same.

2: Two functions in a class are identical and are not allowed.

1: Compile run, execute the method of subclass.

2: Using the parent class method, in the subclass method, use Super. The parent class method name.


    1. Class Father {

    2. String name;


    3. void Eat () {

    4. System.out.println ("Eat litters");

    5. }

    6. }


    7. Class Son extends Father {


    8. public void Eat () {///inheritance enables subclasses to enhance the parent class's methods

    9. System.out.println ("Two side dishes");

    10. System.out.println ("two cups");

    11. System.out.println ("Hot drink");

    12. System.out.println ("Come One");

    13. }

    14. }


    15. Class Demo8 {


    16. public static void Main (string[] args) {

    17. Son s = new Son ();

    18. Methods for executing subclasses

    19. S.eat ();


    20. }

    21. }

3, the phenomenon is called rewriting (overwrite override)

1, in inheritance, a subclass can define the same name as the parent class and a function that has a consistent argument list, which is called a function rewrite

4, the premise

1, there must be an inheritance relationship

5, features

1, when the subclass overrides the function of the parent class, then the object of the child class, if called, must call the overridden function, which can be called by the Super keyword for the overriding function of the parent class.

2, inheritance can make subclasses enhance methods of parent class

6, details

1, the function name must be the same

2, parameter list must be the same

3, when a subclass overrides a function of the parent class, the function must have access permissions greater than or equal to the access rights of the parent class's function or it will compile an error

4, when a subclass overrides a function of the parent class, the return value type must be the return value type of the parent class or the subclass of the return value type, and cannot return a larger data type than the parent class, such as the return value type of the subclass function is Object

1: Define a B class C B extends a

A geta () is defined in the 2:father class;

Rewrite Geta () in the 3:son class; method, attempting to modify the return value to B,c, Object

1:b compiled by

2:C compilation failed with no inheritance relationship

3:object compilation failed, larger than the return value type of the parent class




  1. Class A {


  2. }


  3. Class B extends A {


  4. }


  5. Class C {


  6. }

  7. Class Father {

  8. String name;


  9. void Eat () {

  10. System.out.println ("Eat litters");

  11. }


  12. Define a function to get an object of Class A,

  13. A Geta () {

  14. return new A ();

  15. }


  16. }


  17. Class Son extends Father {


  18. public void Eat () {///inheritance enables subclasses to enhance the parent class's methods

  19. System.out.println ("two cups");

  20. System.out.println ("Two side dishes");

  21. Super.eat ();

  22. System.out.println ("Come One");

  23. }


  24. Class B is a subclass of Class A

  25. B Geta () {

  26. return new B ();

  27. }

  28. }


  29. Class Demo8 {


  30. public static void Main (string[] args) {

  31. Son s = new Son ();

  32. S.eat ();


  33. }

  34. }

7, sub-class object Lookup property or method order

1, principle, proximity principle

If the object of the subclass call method, the default is to use this to find, if the current object does not find a property or method, the current object to maintain the Super keyword point to the object, if you have not found the mutation error, find the direct call,

8, overloads and overrides are different

1, heavy Duty (overload)

1, assuming that all overloaded functions must be in the same class

2, features

Function name is the same, parameter list is different, not related to other (access control, return value type)

3, different

The number is different, the order is different, the type is different

2, overriding (override)

1, premise, inheritance

2, features

Function names must be the same, parameter lists must be the same

The return value type of the subclass is equal to or less than the return value of the parent class


What is rewriting 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.