Detailed Java rewrite

Detailed Java rewrite

Ⅰ. What is rewriting?

Rewriting is the subclass rewriting the implementation process of the parent class's allowed access method, and the return value and formal parameters cannot be changed. That is, the shell remains unchanged, and the core is rewritten!
The advantage of rewriting is that subclasses can define their own specific behaviors as needed. In other words, subclasses can implement the methods of the parent class as needed.

public class FOREVER_GWC_FIRST 
{
	
	public void FOREVER_GWC()
    {
		System.out.println("FOREVER_GWC_FIRST");      
	}

}
public class FOREVER_GWC_SECOND extends FOREVER_GWC_FIRST
{
	
	public void FOREVER_GWC()
    {
		System.out.println("FOREVER_GWC_SECOND");      
	}
	
}
public class FOREVER_GWC_THIRD 
{
	
	public static void main(String[] args) 
	{
		
		FOREVER_GWC_FIRST first = new FOREVER_GWC_FIRST();
		FOREVER_GWC_SECOND second = new FOREVER_GWC_SECOND();
		
		first.FOREVER_GWC();
		second.FOREVER_GWC();
	}
	
}

The results are as follows:

FOREVER_GWC_FIRST
FOREVER_GWC_SECOND

Ⅱ. Why rewrite?

Features benefit
Encapsulation Security and reuse
Inheritance Efficiency and reusability
Polymorphism Unity and efficiency

If the subtype rewrites the method of the same name of the parent type, the method of the subtype can be called only by knowing the definition of the parent type, which enhances the reusability and reusability of the class, which reflects the idea of ​​software reuse.

Ⅲ. How to rewrite?

  • The parameter list must be exactly the same as the method being rewritten.
  • The return type and the return type of the overridden method can be different, but must be derived from the return value of the parent class.
  • The access rights cannot be lower than those of the overridden methods in the parent class. For example: if a method of the parent class is declared as public, then overriding the method in the child class cannot be declared as protected.
  • The member methods of the parent class can only be overridden by its subclasses.
  • Methods declared as final cannot be overridden.
  • Methods declared as static cannot be overridden, but can be declared again.
  • The child class and the parent class are in the same package, then the child class can override all methods of the parent class, except for the methods declared as private and final.
  • The child class and the parent class are not in the same package, so the child class can only override the non-final methods of the parent class declared as public and protected.
  • Overridden methods can throw any non-mandatory exceptions, regardless of whether the overridden methods throw exceptions. However, overridden methods cannot throw new mandatory exceptions, or wider mandatory exceptions than those declared by overridden methods, and vice versa.
  • The constructor cannot be overridden.
  • If you cannot inherit a method, you cannot override this method.
Published 19 original articles · praised 0 · visits 1616

Guess you like

Origin blog.csdn.net/FOREVER_GWC/article/details/104789033