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