Java method rewriting and java method Rewriting

Source: Internet
Author: User

Java method rewriting and java method Rewriting

Method Rewriting:

1. You can rewrite the methods inherited from the base class as needed in the subclass.

2. The method to be rewritten and the method to be overwritten must have the same method name, parameter list, and return type.

3. The method to be rewritten cannot have more strict access permissions than the method to be rewritten.

Program code:

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;    }        public String getInfo(){        return "Name is:"+name+",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 person = 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());    }}

Execution result:

 


In what situations does JAVA require method rewriting?

1. To implement an interface, you must override the method in the interface.
(If the interface method is the same as the method in the Object)
2. inherit an abstract class and must overwrite the abstract methods in the abstract class.
(If the methods in the abstract class are the same as those in the Object)
3. inherits a parent class. When the method of the parent class cannot meet the requirements of the subclass,
Override methods inherited from the parent class

Override method: The method names must be consistent,
The parameter type must be ensured. The number of parameters is the same as the return type.
Of course, when the return type is an object type, the return type of the override method can be a subclass type of the object.
The access permission of the override method cannot be smaller. A larger detected exception cannot be thrown.
(Note: if the parent class does not throw an detected exception, the Child class cannot throw or can only throw a runtime exception)

This is my personal summary.

Java Virtual Methods and rewriting

C #'s Virtual Function concept comes from C ++. For people like me who directly learn java, it is not a good thing to understand, and there is a conflict with java's object-oriented. Similar keywords are not found in java. Java polymorphism has two important concepts: overriding and overloading ). Note: It is not a keyword. By the way, an anonymous friend has mixed up the two functions. The functions with the same name and different parameter types are overloading, which is irrelevant to the return type. In my understanding, you can think that all overriding methods in java are virtual, and all rewrite methods are override. For example. Class A {void func () {System. out. println ("func in A") ;}} class B extends A {void func () {System. out. println ("func in B") ;}}there are two classes, the following code will be executed, and the effect of adding virtual and override to C # is the same. A a = new A (); A B = new B ();. func (); // output func in AB here. func (); // output func in B
 

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.