Rewriting of Java methods

Source: Internet
Author: User

Override of Method:

1. In subclasses, you can override the methods inherited from the base class as needed.

2. The overridden method and the overridden method must have the same method name, argument list, and return type.

3. The overriding method cannot use more restrictive access than the overridden method.

Program code:

classperson{Private intAge ; PrivateString name;  Public voidSetage (intAge ) {         This. Age =Age ; }     Public voidsetName (String name) { This. Name =name; }     Public intGetage () {returnAge ; }     PublicString GetName () {returnname; }         PublicString GetInfo () {return' Name is: ' +name+ ', age is ' +Age ; }}classStudentextendsperson{PrivateString School;  Public voidSetschool (String school) { This. School =School; }     PublicString Getschool () {returnSchool; }     PublicString GetInfo () {return"Name is:" +getname () + ", +getage () +", School is: "+School; }} Public classtestoverride{ Public Static voidMain (String args[]) {Student Student=NewStudent (); Person Person=NewPerson (); Person.setage (1000); Person.setname ("Lili"); Student.setage (23); Student.setname ("Vic"); Student.setschool ("Shnu");        System.out.println (Person.getinfo ());    System.out.println (Student.getinfo ()); }}

Execution Result:

Rewriting of Java methods

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.