Abstract concept
The same method function declarations, but different methods of body functions. Then the time can be extracted, but only extraction method statement, not subject extraction method. Then this method is an abstract method.
Abstract class definition format
A: abstract methods defined format:
a: public abstract method return type name (parameter);
Abstract class defines the format:
abstract class 类名 {
}
Example :
1 // abstract class method must be declared abstract 2 public abstract class Test1 { . 3 . 4 // abstract methods, can not have "{}" . 5 public abstract void F (); . 6
In addition to abstract class contains abstract methods, but may also comprise specific variables and specific method. Contain class without abstract methods, may be declared as an abstract class, it is instantiated prevented.
An abstract class can not be instantiated, i.e. not use the instance of the new keyword to obtain an abstract class, an abstract method must be implemented in a subclass.
Abstract application:
1 // abstract class method must be declared abstract 2 public class Demo01 { . 3 . 4 public static void main (String [] args) { . 5 Teacher Teacher = new new Teacher ( "teacher" ); . 6 teacher.work () ; . 7 . 8 driver = driver new new driver ( "driver" ); . 9 driver.work (); 10 } . 11 12 is } 13 is // an abstract class 14 abstract class People { 15 // abstract method 16 public abstract void work(); 17 } 18 class Teacher extends People{ 19 private String work; 20 public Teacher(String work) { 21 this.work=work; 22 } 23 @Override 24 public void work() { 25 System.out.println("我的职业是"+this.work); 26 } 27 28 } 29 class Driver extends People{ 30 private String work; 31 public Driver(String work) { 32 this.work=work; 33 } 34 @Override 35 public void work() { 36 System.out.println("我的职业是"+this.work); 37 } 38 }
Polymorphism
definition
Polymorphism is the ability to have several different forms or manifestations of the same behavior. Polymorphism is the ability of the same instance of a different interface to perform different operations use
The final state reflects the multiple reference variable can point to a parent class subclasses the object. Polymorphism is the premise must have child parent relationship or relationship class implements an interface, or can not complete the polymorphism.
When the polymorphic method call parent class reference variable, calls the method after the child class overrides.
Format: that is the parent class reference variable subclass object point
Parent class type variable name = new subclass type ();
The variable name method name ().;
Polymorphic features members
1. Polymorphic member variables: Look left compile and run
Fu f=new Zi();
System.out.println (f.num); // f is a value Fu is only accessible to the value of the parent
2. Multi-state member method: Compile look left, look to the right to run
Fu f1 = new Zi ();
System.out.println (f1.show ()); // f1 is the type of facade Fu, but the actual type is Zi, so the method call is rewritten.
instanceof keyword
Action: to determine if an object belonging to a data type.
Note: The return type is boolean
Example:
. 1 Fu F1 = new new Zi (); 2 Fu F2 = new new Son (); . 3 IF (F1 the instanceof Zi) { . 4 System.out.println ( "Zi is the type F1" ); . 5 } . 6 the else { . 7 the System. out.println ( "f1 is the Son type" ); 8 }
Polymorphic transition
Polymorphic transition into two kinds of transition upward and downward transition
upward transition : the process itself is polymorphic transformation upwardly through the
Using the format: parent class type variable name = new subclass type ();
Application scenarios: When no face subclass type, by increasing the extensibility, or use the functionality of the parent class to complete the corresponding operation.
Downcast : a transition has upwardly subclass object casts format may be used, the type of the parent class reference into various types of sub-class reference
Using the format: variable name = type subclass (subclass type) parent class type of variable;
Applicable scene: When you want to use a subclass-specific features.