Java day4 [override] Chapter XIII

Thirteen override:

1. Method override:

(1) subclass inherits the parent class, but may also occur inappropriate

  Subclass deficiencies found in the parent class design and the need to retain the properties and methods of the parent class name, in which case the method will be overwritten.

  Methods override: Like the parent class method

eg: override observation method:

Channel class 
{ 
	public void Connect () { 
		System.out.println ( "[Chnanel parent] connection resource"); 
	} 
} 
class DatebaseChannel the extends Channel 
{ 
	public void Connect () { 
		System.out.println ( "[Sub class] connection database resources "); 
	} 
} 

public class stringdem { 
	public static void main (String args []) { 
		DatabaseChannel new new DatabaseChannel Channel = (); 
		channel.connect (); 
	} 
}

(2) overwrite meaning:

 

 

(3) If a subclass has been covered, to call the parent class, use super. Method

Channel class 
{ 
	public void Connect () { 
		System.out.pringln ( "[Channel] resource link"); 
	} 
} 

class DatebaseChannel the extends Channel 
{ 
	public void Connect () { 
		super.connect (); 
		System.out.println ( " [subclass] connection database resources "); 
	} 
} 

public class Stringdemo { 
	public static void main (String args []) { 
		DatabaseChannel new new DatabaseChannel Channel = (); 
		channel.connect (); 
	} 
}

2. The method override restrictions:

(1) method was overridden can not have more stringent than the parent class method of access control rights:

  public> default (not write)> private

private attributes may be defined in addition, the method can also define:

 

 

 

 

 

  private class is not visible, it does not involve overwriting

  In the actual development in the future as long as the method is defined, ninety-five percent are using public

 

(3) Properties covered:

  Subclass defines members of the same name of the parent class.

(4) final Keyword:

  It represents a final end connector in the program

  final: can not be inherited class

     The method can not be overridden

     constant

  eg: the class can not be inherited:

  

final class Channel(){}

  eg: the method can not be overwritten:

public final void fun(){}

  Focus! ! ! !

  Define constants can not be modified:

private final int ON = 1; // Open the switch represented by a 
private final int OFF = 0; // 0 indicates a switch closure

  Above ON, OFF can not be modified.

Guess you like

Origin www.cnblogs.com/xiwenxinaini/p/11689592.html