Polymorphic ★★★★★ (one of the object-oriented features): The function itself is polymorphic, and a certain thing has different concrete embodiment.
embodiment: A reference to a parent class or interface refers to its own subclass object. Animal a = new Cat (); The parent class can call a subclass of the override (a method in the parent class)
The benefits of polymorphism: It improves the extensibility of the program. The inherited parent class or interface is typically something in a class library, (if you want to modify the way a method is implemented) only by subclasses to overwrite a method that you want to change, so you can invoke the override method by pointing the application of the parent class to the instance of the subclass!
The disadvantage of polymorphism: when a parent class reference points to a subclass object, the extensibility is improved, but only the methods available in the parent class can be accessed, and the methods specific to the subclasses are not accessible. ( late-generation functionality cannot be used in the prophase, that is, access limitations )
Prerequisites for polymorphism:
1: Must have a relationship, such as inheritance, or implementation.
2: There is usually a overwrite operation.
If you want to use the subclass object's unique method, how to determine which object is the specific subclass of the class?
Can be instanceof by a keyword ;//Determines whether the object implements the specified interface or inherits the specified class
Format:< object instanceof Type > Determines whether an object belongs to the specified type.
Student instanceof person = true;//student inherits the Person class
----Java.lang.Object
object: the direct or indirect parent of all classes, Java believes that all objects have some basic common content, which can be extracted continuously upward, and finally extracted to a topmost class, defined in this class is the functionality of all objects.
Specific methods:
- Boolean equals (Object obj): used to compare two objects for equality, in fact two object addresses are compared internally.
2,string toString (): turns the object into a string; The format returned by default: class name @ hash value = GetClass (). GetName () + ' @ ' + integer.tohexstring (hashcode ())
In order to make sense of the object's corresponding string content, it is possible to create a unique string representation of the class object by means of replication.
Public String toString () {
Return "Person:" +age;
}
3,class getclass (): Gets the byte-code file object to which any object runtime belongs.
4,int hashcode (): Returns the hash code value of the object. This method is supported to improve the performance of the hash table. The internal address of the object is converted to an integer.
Usually Equals,tostring,hashcode, in the application will be replicated, to establish specific objects of the unique content.
Inner class: If Class A requires direct access to members in Class B, then Class B needs to establish an object of Class A. In this case, the Class A is defined directly in class B for ease of design and access. You can do it. Class A is called an inner class . An inner class can directly access members in an external class. For external classes that want to access internal classes, you must create an object of the inner class.
classouter{intnum = 4; classInner {voidShow () {System.out.println ("Inner Show Run" +num); } } Public voidmethod () {Inner in=NewInner ();//creates an object of the inner class. in.show ();//the method that invokes the inner class. //the inner class accesses the external class member directly, using its own instance object; } //an external class accesses an inner class object to define an inner class;
You can use some member modifiers to decorate private and static when the inner class definition is on a member location in an external class.
You can use some member modifiers to decorate private and static when the inner class definition is on a member location in an external class.
1: Default modifier.
Direct access to inner class format: external class name. Internal class Name Variable name = External Class object. Inner class object;
Outer.Inner in = new outer.new Inner ();//This form is seldom used.
But this is a rare application because internal classes are defined internally for encapsulation. to get an inner class object is usually obtained by means of an external class. This allows the inner class object to be controlled.
2: Private modifier.
Usually the inner class is encapsulated and will be privatized because encapsulation does not allow direct access by other programs.
3: Static modifier.
if the inner class is statically decorated, which is equivalent to an external class , access limitations occur and only static members in the outer class can be accessed.
Note: If a static member is defined in an inner class, the inner class must be static.
The internal class is compiled with the file name: "External class name $ internal class name. Java";
Why does an inner class have direct access to members in an external class?
That's because the inside holds a reference to an external class. This is a reference to the external class name.
An inner class can be defined on a member location in an external class, or in a local location in an external class.
When an inner class is defined at a local location, only local variables that are final decorated locally are accessible.
Anonymous inner Class (object): an inner class without a name. is the simplified form of the inner class. It is usually used only once to use this form. An anonymous inner class is actually an anonymous subclass object . to define an anonymous inner class: The prerequisite is that the inner class must inherit a class or implement an interface.
anonymous Inner class format:new parent class name & Interface name () {defines a subclass member or overrides a parent class method}.
Usage scenarios for anonymous inner classes:
When the parameter of a function is an interface type reference, if the method in the interface does not exceed 3. The passing of parameters can be accomplished through anonymous inner classes.
In fact, when you create an anonymous inner class, the encapsulation method in the class is not too much, preferably two or two or less.
--------------------------------------------------------
Interview
//1
New Object () {
void Show () {
System.out.println ("show Run");
}
}. Show (); It's no problem writing and compiling.
//2
Object obj = new Object () {
void Show () {
System.out.println ("show Run");
}
};
obj.show (); Correct wording, compile will error
Are the 1 and 2 correctly spelled? Is there a difference? Say why.
The notation is correct, and both 1 and 2 are the subclass objects of an object class created by an anonymous inner class.
Difference:
The first one is compiled through, and runs.
The second compilation fails because the anonymous inner class is a subclass object that is promoted to the object type when the obj reference to object is pointed to, and compile to check if there is a show method in the object class, at which point the compilation fails.
Java Basics-Polymorphic