Java method Rewrite instance analysis _java

Source: Internet
Author: User

The example of this article tells the Java method rewrite, share for everybody for reference. The specific analysis is as follows:

An overview of the methods of rewriting:

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, parameter list, and return type.

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

Second, the program code is as follows:

Class person{private int age;
  
  private String name;
  public void Setage (int age) {this.age = age;
  public void SetName (String name) {this.name = name;
  public int getage () {return age;
  Public String GetName () {return name;
  The public String GetInfo () {return ' Name is: ' +name+ ', the age is ' +age;
  
  Class Student extends person{private String school;
  public void Setschool (String school) {This.school = school;
  Public String Getschool () {return school;
  Public String GetInfo () {return ' Name is: ' +getname () + ', Age is ' +getage () + ', School is: ' +school;
    } public class testoverride{public static void Main (String args[]) {Student Student = new Student ();
    person who = new person ();
    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 ());

 }
}

The results of the execution are shown in the following illustration:

I hope this article will help you with your Java programming.

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.