Rewrite (overwrite) the method Java -

2.2 Method of rewriting (overwriting) (override, orverwrite)

2.2.1 When the method to be rewritten?

If the method in the parent class has been unable to meet the business needs of the current sub-class method of the parent class will be required to re-write it again. It is to change the behavior of the parent class.

Note: After a subclass if the parent class is overridden subclass object must call the method after the rewrite.

2.2.2 Method coverage condition occurs

① occurs between two classes with inheritance relationship

② must have the same method name, return type is the same, the same list of parameters.

③ overridden method can not have access to less than the method being overridden.

④ overridden methods can not throw exceptions broader than the method being overridden. (Repeat exception mechanism.)

⑤ proprietary method can not be overridden. (After speaking polymorphism)

⑥ constructor can not be overwritten. Because the constructor can not be inherited.

⑦ static method does not exist to cover. (After speaking polymorphism)

⑧ coverage refers to the method-independent member, and member variables.

Inheriting the basic functions: code reuse. Inherit the most important role: the method can be overridden.

The difference between overloading and method of rewriting 2.2.3

The method of overload ( Overload ) by:

① method with the same name

② method parameter types, the number, at least one different sequence

③ the return type may be different, since the type of method overloading and returns nothing

④ modifier method may be different, because there is no method overloading and modifiers relations

⑤ method overloading only appear in the same class

The method of covering (Override) by:

① must be inheritance

② covering only appear in a subclass, if there is no inheritance relationship, there is no cover, there is only reload

③ overridden methods, and the parent class must be exactly the same way in subclasses, i.e. the method name, return type, parameter list, exactly the same

④ access to the subclass method can not be less than the access permissions of the parent class method

⑤ subclass method can not throw more exceptions than the parent class method, but the parent class method can throw an exception of an abnormal child

⑥ parent class static methods can not be overridden by subclasses

⑦ private methods can not override the parent class

⑧ coverage is for the members of the method, rather than the property

Note: The two methods to the same static or with a non- static .

public class Test01{

       public static void main(String[] args){

              // create a subclass object

              Cat c = new Cat();

              // call the method

              c.move();

              Animal a = new Animal();

              a.move();

       }

}

// parent: Animals

class Animal{

       // member method

       public void move(){

              System.out.println ( "Animal moving!");

       }

}

// subclasses: Cats

class Cat extends Animal{

       move should output method // Cat: Cat in the catwalk!

       The move method // Animal been unable to meet the Cat.

       // so to move to redefine methods in the class Cat Animal class (coating method, a method of rewriting, Overwrite, Override)

       public void move(){

              System.out.println ( "Cat in the catwalk!");

       }

}

 

 

 

 

Guess you like

Origin www.cnblogs.com/superjishere/p/11809949.html