Java basics-method overloading and method Rewriting

Source: Internet
Author: User
1. Method overload)

Method overloading means that multiple methods have the same name, but different method parameters. Different method parameters have two meanings: one is that the parameter types are different, and the other is that the number of parameters is different. A typical example is that one of our classes can have many constructor methods. By default, the constructor without parameters is used. The Return Value of the method has no effect on the overload. In the following example, compilation fails:

Public int sayhello () {return 1;} public Boolean sayhello () {return false ;}
2. Override)

Override of a method is also called overwrite. The return type of a subclass and its parent class is the same, the method name is the same, and the parameters are the same. In this way, the Child class and the parent class constitute an overwrite relationship.

Public Class Parent {/*** sayhello () method of the parent class */Public void sayhello () {system. out. println ("hello") ;}} public class Child extends parent {/** subclass overrides the sayhello () method of the parent class */@ overridepublic void sayhello () {system. out. println ("Hello World ");}}

In fact, there is a simple distinction between method overloading and method Rewriting:

1. Method overloading occurs in two or more methods of the same class;

2. Method rewriting occurs between the parent class and the subclass.

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.